tags:

views:

42

answers:

2

I'm trying to replace the [2] in the name values of the hidden fields with a new value within a loop. The 2 could be any number and so I won't necessarily know it when I'm looping. What would be the best course of action in replacing it?

<input type="hidden" class="pid" name="t[2][p][149][id]" value="ID">
<input type="hidden" class="pane" name="t[2][p][149][name]" value="NAME">
+1  A: 

If it will always be the patten "t[number][..." you should create a regular expression and replace it that way.

MikeG
Note that he asked for "the best course of action in replacing it" not "tell me exactly how to write the code to replace it" !
MikeG
Yes. But, no reason not to go above and beyond. Plus, it's much easier to work out the english from the code, than the reverse.
sje397
+3  A: 
$("input").each(function() {
  name = $(this).attr("name");
  name = name.replace(/^t\[\d+\]/, "t[your number]");
  $(this).attr("name", name);
});
elektronikLexikon
From the question: *"The 2 could be any number and so I won't necessarily know it when I'm looping."*
T.J. Crowder
I just edited it.
elektronikLexikon
Cool. Would also recommend not assuming all names should be processed... :-)
T.J. Crowder
Still, won't work when the 'any number' has more than a single digit.
sje397
now it does :-)
elektronikLexikon