Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am using jQuery UI Sortable for Drag/Drop re-ordering of rows in a table, and it's working fine.

However, the inputs in the rows have a counter which is their position in the table. They are of the format:

td
    input(class="form-control itemDesc" type="text" name="items[0][desc]")
    input(class="form-control rowQty" type="number" step="any" name="items[0][qty]" value="1")
etc..

I cannot change the formatting of this.

I will be passing a callback function to be run on the completion of the drag/drop, which checks the new position of the row in the table, and that will be my value to use to replace the [i].

How would I manipulate the number between the square brackets? How would I perform the replacement?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

If by 'replace the [i]' you are referring to the index value inside the name attribute then it's just simple string manipulation.

This will work.

$('td input').each(function(index, element) {
    var name = element.getAttribute('name');
    element.setAttribute('name', name.slice(0, name.indexOf('[') + 1) + newIndexValue + name.slice(name.indexOf(']'), name.length));
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.