Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the actual value attribute of an input with jQuery

I am trying to reset the actual value attribute of an input so that it can be reset back to that value if reset, not necessarily the original value when the form was loaded. The problem is these changes don't seem to take effect. I have tried both:

$(this).attr('value', $(this).val());

as well as....

$(this).val($(this).val());

Neither of these actually change the value="" attribute of the input, which is what is used when a form is reset. The problem is that if someone saves a form (via AJAX), then again edits it and presses reset (w/out saving this time), it will go back to the original values the page loaded with, NOT the values last saved with, which is what I'm trying to get. I know there are ways around this (store in local variables or something), but a jQuery solution would be much better. Any ideas??

like image 769
Ryan Avatar asked Oct 28 '09 01:10

Ryan


1 Answers

when you are doing something like this:

$(this).val($(this).val());

it really translates as:

var temp = $(this).val());
$(this).val(temp);

So in essence, its just putting the same value back in.

What you will want to do is on the document load or the AJAX request, store the value in a variable and then retrieve it when the form is reset.

like image 182
Daniel A. White Avatar answered Oct 14 '22 12:10

Daniel A. White