input form data resetting (during jquery sortable & html change)
So right now I am using jquery and jquery ui
I have a sortable list of input data (e.g. single line text input fields)
While I know this isn't a great way to update my index arrays, of position, and some of my form data. It was the first thing I got working after a few hours.
However, my contents of the for loop. Specifically line 5 and 6, is whats causing me issues.
If i leave them there, everything updates just fine during sortable change. but all my partially entered user form data resets back to default
If i comment out line 5 and 6. Everything stays in form data, and it is resortable. but i can't update the position[values]
my regex is basically looking for [digit] and replacing it with [newdigit]
I explicitly only use the [] in the places that i need/want updated currently.
- $("#p_scents tbody").sortable({
- stop: function (event, blob) {
- var a = $('#p_scents tbody').children();
- for (var i = 0; i < a.length; i++) {
- var updated = a.eq(i).html().replace(/\[\d+]/, '[' + i + ']');
- $(a).eq(i).html(updated);
- }
- },});
Any thoughts or suggestions, on what I am doing wrong here? I am fairly new to jquery so I am not sure if there is a simple, clever way to handle this.
additional information, for source html
- <table id="p_scents">
<thead>
- //blah blah
- </thead>
<tbody id="p_scents2">
- //a bunch of <tr> rows which I am sorting, this part works fine
- </tbody>
- </table>
Also the [] is contained in the name of it, so its like
- <input class="text-box single-line" name="[' + i + '].QuestionText" type="text" value="" />
for example
...
edit, while I did have id values for them, I think I was modifying them. and I dont really need too.
I think the id may have been contributing.
but i found a really sloppy work around.
Will investigate further why this is the case
here is my functioning draft
- for (var i = 0; i < a.length; i++) {
- $('#p_scents tbody').children().eq(i).find('input').each(function () {
- var temp = this.name;
- temp = temp.replace(/\[\d+]/, '[' + eval(i) + ']');
- this.name = temp;
- });
- $('#p_scents tbody').children().eq(i).find('select').each(function () {
- var temp = this.name;
- temp = temp.replace(/\[\d+]/, '[' + eval(i) + ']');
- this.name = temp;
- });
- var temp = $('#p_scents tbody').children().eq(i).children().eq(0).html().replace(/\[\d+]/, '[' + eval(i) + ']');
- $('#p_scents tbody').children().eq(i).children().eq(0).html(temp);
- }