<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
function copyit()
{
// the original field:
var ref_Fld1 = $("#txt1");
alert(ref_Fld1.val());
//the form where I want to copy it to:
var ref_Form = $("#my_form");
alert(ref_Form.attr('id'));
// I clone the field:
ref_New = ref_Fld1.clone();
// I look at its ID:
alert(ref_New.attr('id'));
//I change its id:
ref_New.attr('id',"new_" + ref_New.attr('id'))
ref_New.attr('name',ref_New.attr('id'));
// I confirm the new id and the other parts of the field:
alert(ref_New.attr('id'));
alert(ref_New.attr('type'));
alert(ref_New.attr('value'));
alert(ref_New.attr('name'));
//I append it to the form .. which moves it there..and I can see it..
ref_New.appendTo(ref_Form);
//This where I get lost... here I change the value .. but I am changing the original one
var new_ref = $("input:#txt1, ref_Form");
new_ref.val("I changed the OLD one. I wanted to change the NEW one?!");
alert(new_ref.val());
//And I notice that the cloned field then disappears after the function finishes???
}
function lookagain()
{
the_form = $("#my_form");
alert($("input:#txt1, the_form").val());
}
</script>
</head>
<body>
<br>
<a href="" onClick="copyit();">Go</a><br>
<a href="" onClick="lookagain();">Look</a><br>
<hr>
<div id="my_fields">
<br>
Checkbox:<input id="chk1" name="chk1" type="checkbox"/>
<br>
Textbox:<input id="txt1" name="txt1" type="text" value="John Smith" />
</div>
<br><br>
<hr>
<form name="my_form" id="my_form">
I want to copy the fields & values to here:
<br>
</form>
</body>
</html>
|