bdo334
bdo334

Reputation: 408

Changing input array name with jQuery

I've some fields that use an array as name like this :

<select name="attr[address][#ID]">
<textarea name="address[#ID][cp]" placeholder="CP"></textarea>

I would like to change the therm "#ID" of all the fields with an unqiue ID using jQuery, is it possible to use something like regex with the .attr() function in order to change the #ID ?

Regards, Adrien

Upvotes: 0

Views: 885

Answers (1)

jfriend00
jfriend00

Reputation: 707228

You can replace all occurrences of #ID in a name attribute with a monotomically increasing number like this:

var cntr = 1;
$("[name*='#ID']").each(function() {
    this.name = this.name.replace(/#ID/, cntr++);
});

Upvotes: 5

Related Questions