Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: html() function get old data in form

Tags:

html

jquery

I am using jQuery to read data in a HTML page. On that page, I have a <form> element which contains some <input> tags for user to enter their values.
When being initialized, this form has some default values for those <input> elements. When I submit this form, I use the html() method to get the HTML string of current state of this page, but I do not why this result string is still contain old values (default input values), not the new ones.
Is there any way to update or persist the user-entered value to those <input> elements before calling html()? How can I get the string which contain newest value by using jQuery methods?
Thank you so much!

like image 877
Đinh Hồng Châu Avatar asked Jul 03 '12 09:07

Đinh Hồng Châu


People also ask

How to get form data using jQuery?

Example of jQuery get form values to get the form data with the serializeArray () function – When we entered the data and click on the button, the output is – In the above code, there is a form that contains some input elements and submits button.

What does htmlstring return in jQuery?

.html( htmlString )Returns: jQuery. Description: Set the HTML contents of each element in the set of matched elements. A string of HTML to set as the content of each matched element. A function returning the HTML content to set.

What is the use of HTML in jQuery?

When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content.

How does jQuery remove content from an element before replacing it?

When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content. Consider the following HTML:


1 Answers

However, it is strange what you are trying to do, but all you need is to update the value attribute of the form elements. It can be done with the following code (put it in the form submit handler):

$("form input").attr("value", function() {
    return this.value;
});

DEMO: http://jsfiddle.net/BXha6/

like image 199
VisioN Avatar answered Oct 01 '22 19:10

VisioN