I have a big tree. I want change all the ul css which id like 'a + num + b + num'(num range from 1 - 99) now it is hard to add a class to all the ul, so I tried use this code to match the id But it caused some wrong `Uncaught Error: HIERARCHY_REQUEST_ERR: DOM Exception 3 ` where is the problem? Thanks. jQuery(document).ready(function(){ var divArray = $('.demo').children('ul'); var id = $('.demo').children('ul').attr('id'); for (mydiv in divArray) { if (id.match(/^a\d{1,2}b\d{1,2}$/)) { $(mydiv).css({...}); } } }); Code (markup): <div class="demo"> <ul id="a1b77"></ul> ... <ul id="a12b21"></ul> <ul id="a12b22"></ul> ... <ul id="a22b1"></ul> ... <ul id="a22b101"></ul>/* not suit num range from 1 - 99, do not change the css style */ </div> HTML:
you can't use attr('id'); attr is only used for tags like attr('a') for <a></a> or attr('img') for <img /> etc
You are mistaken, attr() is used to select attributes from elements, not select the elements themselves. As the id is an attribute of an element it is possbile to select its id, however it only returns the id of the first selected element.
try this one... jQuery(document).ready(function(){ $('.demo ul').each(function(idx, element) { var id = $(element).attr('id'); if (id.match(/^a\d{1,2}b\d{1,2}$/)) { $(element).css({...}); } }); /* var divArray = $('.demo').children('ul'); var id = $('.demo').children('ul').attr('id'); // you are taking an id of an array of ULs.. it will only return the id of the first element for (mydiv in divArray) { if (id.match(/^a\d{1,2}b\d{1,2}$/)) { $(mydiv).css({...}); } } */ }); Code (markup):