jquery match

Discussion in 'jQuery' started by youlichika, Aug 12, 2011.

  1. #1
    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:
     
    youlichika, Aug 12, 2011 IP
  2. Perpetual infinity

    Perpetual infinity Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    attr()
    Code (markup):

    Perpetual infinity
     
    Perpetual infinity, Aug 12, 2011 IP
  3. programmer_best1

    programmer_best1 Well-Known Member

    Messages:
    282
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    133
    #3
    you can't use attr('id');
    attr is only used for tags like attr('a') for <a></a> or attr('img') for <img /> etc
     
    programmer_best1, Aug 15, 2011 IP
  4. Perpetual infinity

    Perpetual infinity Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    Perpetual infinity, Aug 16, 2011 IP
  5. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #5
    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):
     
    JohnnySchultz, Aug 17, 2011 IP
  6. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #6
    hehe i was about to post very similar code but with "for" loop
    but yeah each() is better :)
     
    tiamak, Aug 17, 2011 IP