1

I have a multiple form inputs and they use arrays in their name. The form can be duplicated by the user. When the form is duplicated name of the input field changes accordingly. Basically this is what I have in my form

<div class="machine-stations">
    <div class="property-container">
      <input name="machine[process][0][system][100][station][52][name]" />
      <input name="machine[process][0][system][100][station][52][price]" />
      <!-- more input fields -->
    </div>
</div>
<div class="machine-stations">
    <div class="property-container">
      <input name="machine[process][1][system][100][station][60][name]" />
      <input name="machine[process][1][system][100][station][52][price]" />
      <!-- more input fields -->
    </div>
</div>
<div class="machine-stations">
    <div class="property-container">
       <input name="machine[process][2][system][100][station][40][name]" />
       <input name="machine[process][2][system][100][station][52][price]" />
       <!-- more input fields -->
    </div>
</div>

What I am trying to accomplish is that I have to change the the number after [process] based on the .machine-stations index number. User can duplicate .machine-stations. If input field is contained in .machine-stations index 0 then the number after [process] should be 0 and so on. Thanks!

3 Answers 3

2

I believe this is what you are looking for: http://jsfiddle.net/GQbCV/1/

var $inputs = $(".machine-stations").find("input");
$inputs.each( function(i) {
    this.name = this.name.replace(/\[process\]\[\d+\]/,"[process]["+i+"]");
});

In the fiddle I changed the numbers in the html so that they could be changed back to 0,1,2.

Since you said the above isn't what you are looking for and haven't clarified, I'll guess again... using your updated html structure. This will restart the index to 0 within each .machine-stations : http://jsfiddle.net/GQbCV/2/

$(".machine-stations").each( function() {
    $(this).find("input").each( function(i) {
        this.name = this.name.replace(/\[process\]\[\d+\]/,"[process]["+i+"]");
    });
});

and a third guess... in this case everything within the first .machine-stations will have 0, everything within the second will have 1, etc. http://jsfiddle.net/GQbCV/3/

$(".machine-stations").each( function(i) {
    $(this).find("input").each( function() {
        this.name = this.name.replace(/\[process\]\[\d+\]/,"[process]["+i+"]");
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Appreciate for the time, but this is not what I am looking for.
That could be this.name = this.name.replace(...) and you have saved creating 2 unnecessary jQuery objects and two method calls on each iteration.
@u54r, then what exactly are you looking for? perhaps you should explain what you want to happen and what you want the outcome to be.
0

You can simply do it using the attr('attributeName',function(index,oldvalue) method

$('.machine-stations input').attr('name',function(i,v){
    return v.replace(/process\]\[\d+/,'process]['+i);
    // or return v.replace(/(process\]\[)\d+/,'$1'+i);
});

FIDDLE

Comments

0

1st jsFiddle

The code started simple and the small function grew lol i didn't acknowledge the question edit til after but check the fiddle. I believe this is the direction somewhat your going in.

Comments

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.