Hello Readers,
If we create clone() of any object using jquery, then jquery provides the clone() method which performs the copy of the matched element (means it copies all the matched elements as well as of their descendant elements and nodes in jquery).
Syntax:
$(selector).clone(true|false)
Parameter:
true : Specifies that event handlers also should be copied.
false : it is default parameter which specifies that event handlers should not be copied.
Code Example :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").clone().appendTo("body");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Clone button</button>
</body>
</html>
In the above code, we use the HTML and jquery clone() method and clone() all p elements and then append them to the body element in jquery.
The other example is when we pass the true parameter into the clone() method then it copies both the elements as well as any event handler attached to it.
$('b').clone(true).prependTo('p');
Code Example :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$('b').click(function () {
alert('Handler for .click() called.');
});
$('b').clone(true).prependTo('p');
</script>
</head>
<body>
<h3>Click on the bold 'Hello' text to see the issue</h3>
<br/>
<b>Hello</b>
<p>, how are you?</p>
</body>
</html>
0 Comment(s)