Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  25] [ 6]  / answers: 1 / hits: 20103  / 13 Years ago, tue, june 28, 2011, 12:00:00

I'm trying to have a variable store the HTML in a div tag, but simply using var a = $('div').html() doesn't store the values of the input tags that lie within the div.



So, my question is, how should I go about saving the HTML and the selected options and values of input tags to a variable using jQuery?



Here is some example code:



HTML:



<div>
<p>Some Text</p>
<select name=word>
<option value=1>Placeholder 1</option>
<option value=2>Placeholder 2</option>
</select>
<input type=text />
</div>


Javascript:



/* a should also have the user values, such that when I use $('body').append(a), 
it has the same user input as the div. */

var a = $('div').html();


Thanks in advance.


More From » jquery

 Answers
22

You could $.clone() the element.



var $a = $(div:first).clone();

$a.appendTo(body); // Clone invades your body


Online Demo: http://jsbin.com/obebov/edit


[#91448] Monday, June 27, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
catrinas

Total Points: 587
Total Questions: 100
Total Answers: 105

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;