Run
Run

Reputation: 57176

jquery: How to select the checkbox that has an array value in its attribute name?

How can I use jquery to select the checkbox that has an array value in its attribute name?

html,

<input type="checkbox" name="delete[]" value="1"/>

My attempt that does not work,

$('input:checkbox[name=delete]',container).not('input:checkbox[disabled=disabled]').prop('checked', true);

Any ideas?

Upvotes: 0

Views: 580

Answers (2)

Anthony Grist
Anthony Grist

Reputation: 38345

In this case, since you're checking an attribute value, you can just wrap the value in double quotes (anything within the quotes is treated literally):

$('input:checkbox[name="delete[]"]')

Or more generally, escape the special characters:

$('input:checkbox[name=delete\\[\\]]');

The method for escaping, and the list of special characters, is covered at the start of the documentation for Selectors.

Upvotes: 1

palaѕн
palaѕн

Reputation: 73896

You can do this:

$('input:checkbox[name^=delete]',container)

or this:

$('input:checkbox[name="delete[]"]',container)

or this:

$('input:checkbox[name=delete\\[\\]]',container)

Upvotes: 1

Related Questions