Regular expression matching problem

Discussion in 'JavaScript' started by colin008, May 21, 2009.

  1. #1
    Hello, hoping an javascript guru can can help..

    I have this function to check and uncheck a large number of checkboxes..
    function CheckAll(form){
            for(var i=0;i<form.elements.length;i++){
                    var e = form.elements[i];
                    if (e.name == 'checkme[]'){
                            e.checked = form.chkall.checked;
                    }
            }
    }
    PHP:
    the form...
    
    print "<form...>";
    print "<input name='chkall' type='checkbox' onclick='CheckAll(this.form);'> Select All";
    $i=0;
    loop...
    print "<input type='checkbox' name='checkme[$i]' value='on' />";
    end loop...
    print "</form>";
    PHP:
    The javascript function looks to find all occurrences of the checkbox name 'checkme[]' but the problem is the form checkbox name is dynamic 'checkme[$i]'

    My question is how can I adjust this line to match the dynamic checkbox name 'checkme[$i]'

    if (e.name == 'checkme[]')
    PHP:

     
    colin008, May 21, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,

    as $i is digit, you could use if ( (/^checkme\[[\d]+\]$/).test(e.name) )
     
    lp1051, May 21, 2009 IP
  3. colin008

    colin008 Active Member

    Messages:
    162
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    68
    #3
    Yes you correct $i is a digit, hehehe that worked a treat. Thanks

    One day i will fully understand JavaScript.
     
    colin008, May 21, 2009 IP