Reputation: 5002
i have many input fields in a table, each input field is like an index of an array like this
<input type="text" name="customproperty[0]" />
<input type="hidden" name="customproperty[0]" />
<input type="text" name="customproperty[1]" />
<input type="hidden" name="customproperty[1]" />
.
.
sometimes client delete fields and sequential order broken;
<input type="hidden" name="customproperty[0]" />
<input type="text" name="customproperty[2]" />..
I want all of them sequential so I call this method each time before post it;
function arrangeInputNames() {
debugger
var inptSlctr = $("#container");
var allinputs = inptSlctr .find("input")
allinputs.each(function (index, el) {
var newIndex = Math.floor(index / 2);//since there is 2 element for each index
el.name = el.name.replace("old name"," new name");//regex or ?
});
}
I need such a regex or any smart business logic to change "only index numbers" i mean the exp above changing shouldn't be "customproperty[2]" to "customproperty[1]" but "2" to "1" since property names always same only indexes can change
Upvotes: 1
Views: 54
Reputation: 2748
Using a regex, you can do :
el.name = el.name.replace(new RegExp("\[[\d]+\]"), "[" + newIndex + "]")
Upvotes: 1