Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding 'divs' using jQuery [duplicate]

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;
  }); 
like image 351
toothie Avatar asked Dec 17 '13 06:12

toothie


People also ask

How to duplicate div in jQuery?

To duplicate a div in jQuery, use the jQuery clone() method.

How append clone in jQuery?

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.

When using clone () which attribute can create conflict?

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.

What is cloning in jQuery?

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.


2 Answers

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.

like image 198
Bk Santiago Avatar answered Sep 30 '22 19:09

Bk Santiago


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

like image 34
Raunak Kathuria Avatar answered Sep 30 '22 18:09

Raunak Kathuria