1

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

1 Answer 1

1

Using a regex, you can do :

el.name = el.name.replace(new RegExp("\[[\d]+\]"), "[" + newIndex + "]")
Sign up to request clarification or add additional context in comments.

1 Comment

I upvoted yet. Someones downvote because in the chart, we have to show an effort of test, and not a free question like "i need something" or "how to do that" etc ......

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.