0

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

1
  • 1
    What do you have? What do you wanna do? Commented Oct 5, 2012 at 15:13

1 Answer 1

5

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++);
});
Sign up to request clarification or add additional context in comments.

2 Comments

+1, you could also pass the index to the each function, that way there's no need for the cntr
@deifwud - yes, it could be done that way. This way allows you to establish whatever baseline you want and/or use the cntr over time to generate other unique IDs. Either is fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.