Alexis
Alexis

Reputation: 16829

How to name inputs when having several jquery raty in one form?

I'm using the jquery raty plugin http://www.wbotelhos.com/raty/ to make a form with star rating.

The plugin generates a hidden field for each star rating input like this :

<input id="question8-score" type="hidden" name="score">
<input id="question6-score" type="hidden" name="score">
<input id="question5-score" type="hidden" name="score">

The is when I validate the form (I use codeigniter) It just get confused with 3 input with the same name. So I want to make a script that do something like :

For each input with name="score", put the id attribute value as the name attribute value.

I think it's probably possible with jQuery but I don't know how to do that. Could someone help me? Thanks!!

Upvotes: 0

Views: 1523

Answers (3)

Pavan
Pavan

Reputation: 4339

$('input[name=score]').each(function(){
    $(this).attr("name", $(this).attr("id"));
});

Upvotes: 0

mplungjan
mplungjan

Reputation: 178285

$(document).ready(function() {
  $('input[name^="score"]').each(function() { // starts with score
    this.name=this.id;
  });
}

Upvotes: 0

paulslater19
paulslater19

Reputation: 5917

$("input[name=score]").each(function () { 
    this.name = this.id;
});

Edited the selector

Upvotes: 4

Related Questions