20

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!

5
  • Why would you want to do that? Commented Aug 17, 2011 at 16:05
  • I need to store a 'snapshot' of the page at the time when it was submitted to generate a PDF from it. Commented Aug 17, 2011 at 16:06
  • When do you get $("body").html()? Commented Aug 17, 2011 at 16:09
  • Then it should give you the markup with updated value. Commented Aug 17, 2011 at 16:15
  • 3
    It doesn't, thus I created this question.. Have you even tried it? Commented Aug 17, 2011 at 16:17

6 Answers 6

20

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I need a way to do this for 'all' input elements on the form.
The OP will have to go through all inputs and change their value using this, but it should work. +1
$("body").children().each(function() { $(this).attr("value", $(this).val());});
Note that this.value will be faster than $(this).val()
@Sarfraz Unless he has hundreds of inputs on the page, the difference will probably be less than a millisecond.
|
18
$('[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()

2 Comments

The accepted answer does not work well with textarea. But this solution works.
This should have been the accepted answer considering the a form does not contain only input element.
2
$('input').each(function(){
  $(this).attr('data-value-input', $(this).val());
});

and after that you can get your html

var bodyHtml = $('body').html().toString().replace('data-value-input', 'value');

and that is it.

2 Comments

And what if a preset value exists in the HTML on page load? You'll have 2 values then, right?
+1 for @Senad to counteract a -1, for a solution that is on the right track
1

Well there is one most simpler way of doing this, by employing the live function on the input.

$(document).ready(function() {

 var checker = false;
    $("input").live("change", function() {
        checker = (this.defaultValue !== this.value);
        if (checker)
            this.defaultValue = this.value;
    });
});

Comments

0

Long way:

  $("input").each(function(){
    $(this).attr("value", $(this).val());
  });
  $("textarea").each(function(){
    $(this).text($(this).val());
  });
  $("input:checked").each(function(){
    $(this).attr("checked", true);
  });
  $("input:not(:checked)").each(function(){
    $(this).removeAttr("checked");
  });
  $("select option[selected]").each(function(i, el){
    $(el).removeAttr("selected");
  });
  $("select option:selected").each(function(i, el){
    $(el).attr("selected", true);
  });

Comments

0

I had to do this for option

$("select option[selected]").each(function(i, el){
    $(el).removeAttr("selected");
});
$("select").each(function(i, el){
  $(this).find('option[value="' + $('#' + el.id).val() + '"]').attr('selected', true);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.