views:

34

answers:

2

I have some HTML code like this:

<input id="fieldname-1" name="field_name[value][1]" value="1" type="checkbox" />
<input id="fieldname-2" name="field_name[value][2]" value="2" type="checkbox" />
<input id="fieldname-3" name="field_name[value][3]" value="3" type="checkbox" />

I want to access this with jQuery like:

$('input[field_name]').change( function() { dosomething(); });

i can not add a class field to do this by calling $('.classname') because it is rendered by the cck module of Drupal and i don't want to add this to the theme layer.

The best thing would be to let my module add a class for every field. but a quicker solution would be to know how to access these fields by jQuery

+3  A: 

You could use the attribute equals selector to do an exact match on the name attribute.

$('input[name="field_name[value][2]"]').change(func...

Or if you wanted all the <input> elements that start with field_name, you'd use the attribute starts with selector.

$('input[name^=field_name]').change(func...

This will select all <input> elements where the name attribute starts with field_name.

Additionally, you can use :checkbox in the selector instead of input if they're all checkboxes.

Also, you could use the same approach but with the ID attribute if you wanted.

$('input[id^=fieldname]').change(func...
patrick dw
Thanx! that worked for accessing all the items :)
FLY
@FLY - You're welcome. :o)
patrick dw
+1  A: 

It seems that you need a fuzzy selector: [name^=value] Something like this might suffice:

$('[name^=field_name]').each(function(index,element){
  $(element).change(function(){})
})
Frost.baka