SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: jquery match

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery match

    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.

    Code:
    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({...}); 
    	} 
    } 	
    });
    HTML Code:
    <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>

  2. #2
    SitePoint Enthusiast Anthony.Barnes's Avatar
    Join Date
    Aug 2011
    Location
    Canberra, Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jQuery provides you with a way to easily extend it to do these sorts of things. I remember an example I saw some years back, although I wasn't able to find it I've modified some code to suit:

    Code JavaScript:
    $.extend($.expr[':'],{
        idMatch: function(elem,index,m) {
            var reg = new RegExp(m[3],"g");
            return reg.test($(elem).attr('id'));
        }
    });

    If you do something like that then you can pass through pretty much any regular expression to check on the ids:

    Code JavaScript:
    var lists = $('ul:idMatch(^a[0-9]{1,2}b[0-9]{1,2}$)');

  3. #3
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also maybe

    Code javascript:
    $(function(){
        $(".demo").children("ul").each(
            function(){
                if($(this).attr("id").match(/^a\d{1,2}b\d{1,2}$/))
                {
                    $(this).css(...);    
                }    
     
            }
        );
    });
    Last edited by paul_wilkins; Aug 15, 2011 at 00:48. Reason: add BBTags


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •