1

I've got a form inside of a table, something like this:

<form action="" method="post">
<table>
<tbody class="rowAdder">
<tr>
  <td><input name='info[5]' /></td>
  <td><input name='other_info[5] /></td>
</tr>
</tbody>
</table>
</form>

I want to clone the 'tr' element and in the clone I want to increment the index values.

PHP natively treats input names with "[]" as arrays. But I haven't found any way to easily play with that index value in jQuery.

I know I can use ("input[name$=']']") to find all elements on the page with names ending in ']' but that doesn't help me. I won't always know what the "name" element will look like, so using a cumbersome string index method won't work alone. Although, perhaps an even more cumbersome method will.

I just want to find the names that are arrays and increment them when adding a row. I hope there is a simple, elegant way to do it.

var $array_inputs = clone.find("input[name$=']']");

That works to get the correct elements, although it is clunky. Now I need to increment 5's to 6's

9
  • you could build a string markup and increment on that on the concatenation, or clone the object like you said and traverse to change the name Commented Sep 23, 2014 at 4:21
  • But there is no method to get current index value? Commented Sep 23, 2014 at 4:22
  • what do you mean? one function to do it all? no. just traverse the intial markup, get the first index, from then on, increment on that by whatever event you need to, (click, etc..), or just simple initialize a simple counter 1, since in the beginning you'll start at one anyway Commented Sep 23, 2014 at 4:24
  • To traverse the initial markup, I need to get the objects that have array names. If their array indexes were seen as some kind of jquery property then I could increment them easily. Commented Sep 23, 2014 at 4:26
  • simple, if the page initial has already several rows, then get their count (row count), from that count, that will be your starting point Commented Sep 23, 2014 at 4:27

1 Answer 1

1

I don't know which is the best way but clearly, it can be done using a bit of this:

<form action="" method="post">
<table>
<tbody>
<tr>
  <td><input name='info[5]' /></td>
  <td><input name='other_info[5]' /></td>
</tr>
</tbody>
</table>
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    var last_child = $('table tbody tr:last-child'); // point the last row
    var children_input = last_child.children('td:eq(0)').children('input'); // get the input tag
    var index_num = children_input.attr('name'); // get its attr name
    var get_index = index_num.split('[').pop().split(']').shift(); // extract the index
    alert(get_index);
});
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

That looks pretty good. The code I'm writing is inside of $(".rowChanger").on("click", ".appendRow", function() {, so $(this) is the tbody.rowChanger. In which case I suppose I could use $(this).children().last() to get the last TR. Note the tr has 2 inputs.
@ButtleButkus yep, you can implement it using that, and then just point it to the first td children input, then get the attribute name, I assume, info[5] will always match other_info[5] right?, the important thing anyway is get the last index, so that will give you the starting point in which next number you will increment
Thanks and if I wanted to get the row count inside my $('tbody.rowChanger'), how would I do it that way? I tried $(this).length but it didn't work. It always says 1 (perhaps not updating with added rows).
Perhaps I've gotten that: $(this).children().length;
@ButtleButkus yes its indeed children().length

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.