This is the script that I've written. The first click function works fine but the other two don't work as expected. The tags follow the href link and the function for its click doesn't work. Is it because they have been created by jQuery and are not in the original html page?
$("#edit-description").click(function(){
$("#add-description").replaceWith( "<div id='add-description'><textarea cols='43' rows='9' placeholder='Add a description...'></textarea><a href='#' class='u-update'>Update</a><a href='#' id='add-description-cancel'>Cancel</a></div>" );
return false;
});
$("#add-description-cancel").click(function(){
$("#add-description").replaceWith( '<div id="add-description">Write a <a href="/#" id="edit-description">description</a> about the college.</div>');
return false;
});
$(".u-update").click(function(){
$("#add-description").replaceWith( '<div id="add-description">Write a <a href="/#" id="edit-description">description</a> about the college.</div>');
return false;
});
To duplicate a div in jQuery, use the jQuery clone() method.
Copy/Paste with clone() & append() using jQuery You can copy an element from one place to another using the clone() function in jQuery. First you make a copy using the clone() function then you use the appendTo() function to indicate where you want the copy placed.
clone() has the side-effect of producing elements with duplicate id attributes, which are supposed to be unique. Where possible, it is recommended to avoid cloning elements with this attribute or using class attributes as identifiers instead.
The clone() is an inbuilt method in jQuery which is used to make a copy of selected elements including its child nodes, text and attributes. Syntax: $(selector).clone(true|false) Parameter: It accepts an optional parameter which could be either true or false specifies that event handler should be copied or not.
If element has been created dynamically use delegate
function of jquery
change your 2nd and third code to.
$("body").delegate("#add-description-cancel", "click", function(){
$("#add-description").replaceWith( '<div id="add-description">Write a <a href="/#" id="edit-description">description</a> about the college.</div>');
return false;
});
$("body").delegate(".u-update", "click", function(){
$("#add-description").replaceWith( '<div id="add-description">Write a <a href="/#" id="edit-description">description</a> about the college.</div>');
return false;
});
Note that you can change the selector $("body")
to something inner like the element's parent that is created during page rendering, so that jquery will limit it's scope in finding it.
Hope it helps.
Use on instead of delegate as on is the preferred method.
As per documentation
As of jQuery 1.7, .delegate() has been superseded by the .on() method
http://api.jquery.com/delegate/
Rule of thumb, whenever you are adding content dynamically use event delegation rather than direct event assignment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With