I need to know how to clone hard coded elements in runtime?
I have a hard coded input field. In run time is it possible to read that elemen开发者_StackOverflowt and make it multiple(append) by user request. Is that possible with jquery or javascript.
Thanks in advance.
Using jQuery clone:
<div class="smallBox">
I'm a small box
<div class="smallInnerBox">I'm a small small inner box</div>
</div>
$('.smallBox').clone().insertAfter(".smallBox");
<div class="smallBox">
I'm a small box
<div class="smallInnerBox">I'm a small small inner box</div>
</div>
<div class="smallBox">
I'm a small box
<div class="smallInnerBox">I'm a small small inner box</div>
</div>
If you're looking for something a little more comprehensive:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
<ul id='itemList'>
<li>
<input class='input' type="text" value="some text"/>
<a class='clone' href='Javascript:;'>Clone</a>
</li>
</ul>
<a id='allValues' href='Javascript:;'>Get Values</a>
<script>
$('a.clone').live('click', function() {
var clonedItem = $(this).parent().clone();
clonedItem.find('.input').attr('value', 'cloned');
$('#itemList').append(clonedItem);
});
$('#allValues').click(function(){
var values = [];
$('.input').each(function(i, text){
values[i] = $(text).val();
});
alert('Values are: ' + values.join(', '));
});
</script>
</body>
</html>
What do you mean by "hardcoded"? is this what you need?
$(document).ready(function () {
$("#something").click(function(){
$("#yourform").append($("#yourinputfield"))
});
})
<div id="divElement">Hello</div>
<button onclick="javascript:cloneElement()">Clone Me</button>
function cloneElement()
{
$("#divElement").clone().appendTo("#divElement");
}
Or: http://jsfiddle.net/UFcTk/
精彩评论