Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery html() does not return changed values

Tags:

I have a form with a textbox like this:

<html>
<head>
    <script type="text/javascript" src="jquery-1.6.2.js"></script>
</head>
<body>
    <input type="text" id="myTextBox" />
</body>
</html>

When I type something in myTextBox, the value is available with $("#myTextBox").val(), but is not represented if i do $("body").html(). How can I grab the html string and the updated form value too? Thanks!

like image 568
Harper Avatar asked Aug 17 '11 16:08

Harper


2 Answers

You can use the .attr() function.

Example:

$("input").each(function(){
    $(this).attr("value", $(this).val());
});

After this you can do the:

    $("body").html();

This should work.

like image 57
Norman Joyner Avatar answered Sep 17 '22 17:09

Norman Joyner


$('[type=text], textarea').each(function(){ this.defaultValue = this.value; });
$('[type=checkbox], [type=radio]').each(function(){ this.defaultChecked = this.checked; });
$('select option').each(function(){ this.defaultSelected = this.selected; });

then

$("body").html()
like image 24
Coisox Avatar answered Sep 20 '22 17:09

Coisox