I wish to clone the innerhtml of an element (#clone) and insert it into another element (#newLocation).
The problem with using clone() is that I will have first delete the elements (if any) currently in #newLocation, then iterate over each element in #clone and append it to #newLocation. It’s not the end of the world, but I’m hoping for a simpler way.
Instead, I thought I’d use html(). Since this won’t preserve events, I would have to delegate the even using ON. I had thought this would work, however, it doesn’t.
Any suggestions what I am doing wrong? Also, think it would be more efficient to use the clone() solution even if I can get html() solution working?
EDIT: Just wrote my very first plugin: http://jsfiddle.net/Wnr65/ which seems to work. Good idea to use? Sure it could be written better.
$(document).ready(function(){
$("#cloneIt").click(function(){$('#newLocation').cloneInnerHtml('clone').css('color', 'red');});
$("#clone a").click(function(){alert("click");});
$("#clone select").change(function(){alert("change");});
});
(function( $ ){
$.fn.cloneInnerHtml = function( id ) {
this.empty();
var $t=this;
$('#'+id).each(function() {$t.append($(this).clone(true));});
return this;
};
})( jQuery );
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Clone INNERHTML</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" language="javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#cloneIt").click(function(){$('#newLocation').html($('#clone').html());});
//Only works on original clone
$("#clone a").on("click", function(){alert("click");});
$("#clone select").on("change", function(){alert("change");});
//Doesn't work at all
$("#newLocation a").on("click", function(){alert("click");});
$("#newLocation select").on("change", function(){alert("change");});
});
</script>
</head>
<body>
<input type="button" value="Clone it" id="cloneIt" />
<p>Original clone is shown below</p>
<div id="clone">
<a href="#">Click Me</a>
<select><option>Hello</option><option>Goodby</option></select>
</div>
<hr />
<p>Insert new clones below.</p>
<div id="newLocation">
<p class="someOldElement">someOldElement</p>
</div>
</body>
</html>
It is normal that your code doesn’t react as you want. When you do
it will bind
clickevent to the elements which match the#clone aselector. Those elements are determined at the moment of the execution. In your case, this line bind the event to the first and only link present in the page.The same rule apply for the code
but the difference here is that, at the moment of execution, there is no
aelement inside#newLocation, so the selection is empty and the handler is not bound at all.Then, when you do
it will get the HTML content from one element and copy it into another element, but it’s only about HTML content, so the event binding still the same as before the “copy operation”.
The on method has different syntax, and only one allow the event delegation:
clonemethod doesn’t remove the elements, here is a working demo http://jsfiddle.net/pomeh/G34SE/HTML code
CSS code
Javascript code
The method also receive a parameter indicating whether event handlers should be copied along with the elements http://api.jquery.com/clone/