0

I have a form which has many elements (e.g. textarea, input, select), after users have entered some data the states of these elements should change.

For example, an input[type="radio"] element will have the attribute checked="checked" if a user has checked it. The value attribute of an input[type="text"] element will contain the text entered by user.

The problem is that the html string returned by $('#form1').html() does not contain these data.

Feel free to take a look at this example:

http://jsfiddle.net/cmNmu/

You can see that no matter what your inputs are, the html returned is still the same (having no attribute data).

Is there any easy way to collect the html including their states?

Thanks in advance.

5
  • Is it important to collect all the html or only the values ? Commented Feb 25, 2011 at 11:23
  • @Krtek: Collecting only the values is easy, but I need all html. Commented Feb 25, 2011 at 11:42
  • It's usually wrong to collect HTML, which is serialised data in text form. Iterate over DOM elements instead to preserve meaning and, incidentally, to make your life a lot easier. Commented Feb 25, 2011 at 12:49
  • I think I understand what you want. May I rewrite your question? Commented Feb 25, 2011 at 13:10
  • @Šime Vidas: Of course. Please feel free to do so. Commented Feb 25, 2011 at 16:15

5 Answers 5

1

use below code getting the value of input type text via jQuery

alert($("input:text").val())
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe you could use the 'onblur' event handler to set the value of the element when you leave it

Comments

1

You should get the value using :

$('#form1').find(':input').val();
$('#form1').find(':radio[name=gender]:checked').val();

if you have multiple input then you can filter them bu their name or class or even id. Then you will need to select input using .find(':input[name=input_field_name]'). My Suggestion is : use name property instead of other property if you want to use form.

Comments

1

People usually use $('#form1').serialize() to get the values. If html() doesn't return both the source and data, I don't think that there is something you can other than manually constructing the full html by looking at the data.

Live demo: http://jsfiddle.net/cmNmu/6/

Comments

1

By using the jQuery formhtml plugin written by gnarf:

jQuery html() in Firefox (uses .innerHTML) ignores DOM changes

The changes in the input elements can be reflected in the html string returned by formhtml().

Thank you very much everyone.

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.