1

I've got several input boxes like so:

<input type="checkbox" name="checkbox_item[500]" />
<input type="checkbox" name="checkbox_item[10]" />
<input type="checkbox" name="checkbox_item[2]" />
<input type="checkbox" name="checkbox_item[1]" />
<input type="checkbox" name="checkbox_item[]" />

So I'm using the following regex pattern checkbox_item\[\d+\] which works nicely in my regex tester but I cannot get it to work with Jquery. This is my Jquery code:

console.log($("input:regex(name, /checkbox_item\[\d+\]/)").length);

I was expecting 4 but instead got 28!

Can somebody shed any light on this?

Thanks

Jerry
  • 70,495
  • 13
  • 100
  • 144
Steven Cheng
  • 1,071
  • 3
  • 14
  • 25
  • 1
    So do you use the [following regex filter for jQuery](http://james.padolsey.com/javascript/regex-selector-for-jquery/)? – VisioN Oct 02 '13 at 10:36
  • ... if you do, try to remove slashes `/` before and after regular expression in the selector, i.e. `:regex(name, checkbox_item\[\d+\])`. – VisioN Oct 02 '13 at 10:41
  • Possible duplicate of http://stackoverflow.com/questions/19095607/jquery-selector-to-get-all-select-dropdowns-with-id-pattern/ – Murali Murugesan Oct 02 '13 at 10:42

3 Answers3

1

I don’t think jQuery natively supports regular expressions in selectors in that way.

But you could use the Attribute Starts With Selector [name^="value"].

CBroe
  • 91,630
  • 14
  • 92
  • 150
1

You can use Attribute Starts with like

console.log($("input[type=checkbox][name^='checkbox_item']").length)

Demo

Satpal
  • 132,252
  • 13
  • 159
  • 168
1

I don't know if i understand it corretly, but you want to get the total elements that begins with checkbox_item?

$("input[name^=checkbox_item]").length

This will give you the total elements that begins with checkbox_item.

more info about this selector here.

nkmol
  • 8,025
  • 3
  • 30
  • 51