jQuery script to get only checkboxes with a given prefix

Go To StackoverFlow.com

0

I'm using the jQuery script listed bellow in order to select a few checkbox inputs.

$(document).ready(function() {
  $("input:checkbox:not(:checked)").each(function() {
    var column = "table #" + $(this).attr("name");                            
    $(column).hide();
  });

  $("input:checkbox").click(function(){
    var column = "table #" + $(this).attr("name");                            
    $(column).toggle();
  });
});

How can I modify the script in order to get only the those checkboxes where there name starts with "col_"?

Thank you!

2012-04-04 17:13
by Psyche


7

You can choose attribute with ^= to indicate prefix $= suffix etc

http://api.jquery.com/category/selectors/

$('input:checkbox[name^=col_]')
2012-04-04 17:14
by Andreas Wong
It works fine. Thank you - Psyche 2012-04-04 17:16
@Psyche you can thank me by accepting my answer :) http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-wor - Andreas Wong 2012-04-04 17:19
I will accept it, but I have to wait 5 more minutes : - Psyche 2012-04-04 17:23
@Psyche ok thanks yo : - Andreas Wong 2012-04-04 17:28
no problem man - Psyche 2012-04-04 17:40
Ads