0

I have inputs with names like this

  <textarea type="text" name="featured[items][58252][hpsummary]" cols="50" class="limit20_120">asdfasdfasfasfdsadfasdfaasdfasfdasfasfasdf</textarea>

and I need to renumber the items I can loop through all the inputs no problem, but I am stuck on how to re number them so that the above would then then be 1 instead of 58252

  <textarea type="text" name="featured[items][1][hpsummary]" cols="50" class="limit20_120">asdfasdfasfasfdsadfasdfaasdfasfdasfasfasdf</textarea>

i can numerate just not sure how to parse it

I was thinking of just splitting the string but i'm not sure if I could use an regex so that I don't have to worry if there are more items on the end or if there is a better way

mcgrailm
  • 17,469
  • 22
  • 83
  • 129
  • I'll just leave this here. Again: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Cfreak Mar 09 '12 at 15:19
  • @Cfreak i'm not trying to parse the html just the name string – mcgrailm Mar 09 '12 at 15:21
  • Maybe use serialize() and post() to send the input names (and values) to the server, parse and re-create them there, then use the results of the post() to replace the inputs? – Dave Mar 09 '12 at 15:29
  • @Cfreak while I was thinking of the same comment this question is only about the stuff *inside* the name attribute, which is sufficiently regular... – Boldewyn Mar 09 '12 at 15:29

1 Answers1

2

Something like this?

$('.limit20_120').each(function(i){
    this.name = this.name.replace(/\d+/, i+1);
});

Working demo - http://jsfiddle.net/ShankarSangoli/4RUxS/

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • that would be fine but hpsummary could be anything and more may follow – mcgrailm Mar 09 '12 at 15:24
  • if there are number further down stream wouldn't that change them as well ? – mcgrailm Mar 09 '12 at 15:33
  • It's a little more verbose, but if you need to restrict the replacement, just add more of the name to the regex, i.e. `this.name.replace(/^featured\[items\]\[\d+\]/, 'featured[items]['+(i+1)+']');` – Chris Pratt Mar 09 '12 at 15:34