2

I need to create the HTML into a pdf on the server and need the HTML from the browser that has a form and other stuff in it. Doing $el.html() just gives an HTML string without input values in the form. How do i get the HTMl string with all the input values included?.

I do not want to append the clone on the browser, i am just interested in the HTML string with all input values to pass to the server.

Thanks!

4
  • jquery clone() maybe good for you Commented Apr 11, 2013 at 13:32
  • So you're trying to produce a PDF that is essentially a picture of the current state of the web page (including input values)? Or do you want to produce a PDF that actually has html code on it? Commented Apr 11, 2013 at 13:33
  • yes, i am trying to create a PDF that is the current state of the web page. Commented Apr 11, 2013 at 13:34
  • @user1108772 clone wouldn't store the input values. Commented Apr 11, 2013 at 13:34

2 Answers 2

4

I think this should work. Simply run this before you call .html():

$("input[type='text']").each(function () {
    var $this = $(this);

    $this.attr("value", $this.val());
});

http://jsfiddle.net/GyrWY/1/

Note: If you need to do this for other input types, they will need to be handled differently. For textarea you would set .text($this.val()), for checkboxes and radio buttons, you would need to set the checked attribute. select elements, you would have to set the selected attribute of the appropriate option.

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

3 Comments

You should use the :input selector to get all types of input, such as text areas, select menus, and checkboxes.
@Barmar But <select value="abc"> isn't really valid. And works even less for checkboxes. Textarea would need to be handled differently as well.
You just beat me to it! :)
4

Maybe this will do it:

$("input").each(function() {
    $(this).attr("value", $(this).val());
});
$("textarea").each(function() {
    $(this).text($(this).val());
});
$("select option").each(function() {
    if ($(this).is(":selected")) {
        $(this).attr("selected", "selected");
    } else {
        $(this).removeAttr("selected");
    }
});
$("checkbox").each(function() {
    if ($(this).is(":checked")) {
        $(this).attr("checked", "checked");
    } else {
        $(this).removeAttr("checked");
    }
});

This sets the appropriate DOM attributes for all types of inputs to match their current state. I hope I didn't miss any.

You might want to make a clone of the entire DOM and operate on it. The above code will affect what happens if the user presses the Reset button of the forms, the current state will be considered the default.

4 Comments

Edited my answer to handle types of input that don't use the value attribute.
@Barmar - Hi, it is not working for ion-checkbox, how to do the same for it
@surendher I don't know what ion-checkbox is.
@Barmar - Thats ok, i have found a solution for it. Thanks

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.