RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12224

How can I bind to multiple elements with the same name in jQuery?

I have about 50 items on my screen. Each line is a form that contains one select box. Each select box has the same name, "permission_action"

My code:

$("#permission_action").change(function() {
    $.ajax({  
      type: "PUT",  
      url: this.form.action,  
      data: "permission[action]=" + $("#permission_action").val()  
    });  
});

only binds to the select box in the first form. I want to bind to all of the items.

Upvotes: 0

Views: 3370

Answers (3)

Nick Craver
Nick Craver

Reputation: 630569

You can bind the ones with the same name, not the same ID, like this:

$("select[name=permission_action]").change(function() {
    $.ajax({  
      type: "PUT",  
      url: this.form.action,  
      data: {"permission[action]" : $(this).val() }
    });  
});

Upvotes: 2

Matteo Riva
Matteo Riva

Reputation: 25060

Use a class instead of an ID, which should be unique.

Upvotes: 4

Nazariy
Nazariy

Reputation: 6088

Use CLASS selector instead of ID, as ID suppose to be unique element.

<input class="permission_action" ... />

in javascript

$(".permission_action").change(...);

Upvotes: 5

Related Questions