Change current Index input dynamic jQuery

I want change current Idx(index) in input name, after remove inputs.

At the beginning of the input names of the is as:

row 1 : airline[0] flight_time[0] flight_number[0]

row 2 : dOfv[]

row 3 : airline[2] flight_time[2] flight_number[2]

now, after remove row 2, i want change input name in other rows like this:

row 1 : airline[0] flight_time[0] flight_number[0]

row 3 : airline[1] flight_time[1] flight_number[1]

DEMO: http://jsfiddle.net/5nykq0e6/

what do i do?

Html:

<div class="one">
    1.<input name="airline[0]">
        <input name="flight_time[0]">
            <input name="flight_number[0]">
    <a href="" class="remove_input">X</a>
</div>
<div class="one">
    2.<textarea name="dOfv[]"></textarea>
    <a href="" class="remove_input">X</a>
</div>
<div class="one">
    3.<input name="airline[2]">
        <input name="flight_time[2]">
            <input name="flight_number[2]">
    <a href="" class="remove_input">X</a>
</div>

JS:

$(document).on('click', 'a.remove_input', function(e) {
    e.preventDefault();
    $(this).closest('div.one').remove();
    
    $('.one').each(function(i, el) {
        //return parseInt($.trim(text)) - 1;
        var text = $(el).text();
        var currentIdx = parseInt(str.match(/\d+/)[0], 10);
        $(el).text('name', str.replace(currentIdx, i));
    })
    
})

Hi,

First question: what do you expect to happen if you remove row 1?
This?

row 1 : dOfv[] 
row 2 : airline[1] flight_time[1] flight_number[1] 

Second question: how many rows do you have? Just three or is this a sample demonstrating a pattern?

The first question: your answer is correct.
Second question:this is a sample demonstrating a pattern.

What is it?

Please give an example in jsfiddle.net.

So whenever a row is removed:

  • If it contained <textarea name="dOfv[]"></textarea> do nothing
  • If it contained <input name="airline[x]"> then reorder the following attributes on the remaining input elements: airline[], flight_time[] and flight_number[]. These attributes should be continuous (i.e. 0, 1, 2, 3 …)

Did I get that right?

Yes, it is right.How it done?

If it contained <textarea name="dOfv[]"></textarea> attributes should be continuous.

Please note example:

i.e. : airline[],textarea:dOfv[],flight_time[], flight_number[]
These attributes should be continuous ( 0, 1, 2, 3)

After remove textarea:dOfv[]:
airline[],flight_time[], flight_number[]
These attributes should be continuous ( 0, 1, 2)

And or After remove flight_time[]:
airline[],textarea:dOfv[], flight_number[]
These attributes should be continuous ( 0, 1, 2)

Etc…

When a remove link is clicked, you could iterate over the input elements like so, making use of the attribute starts with selector:

$("input[name^='airline']").each(function(i, el){
  this.name = "airline[" + i + "]";
});

Then it’s a matter of setting their name attributes to the value of i
You can repeat the process for flight_time[] and flight_number[]

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.