5

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;
  }); 
toothie
  • 1,019
  • 4
  • 20
  • 47

3 Answers3

5

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.

Bk Santiago
  • 1,523
  • 3
  • 13
  • 24
3

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

Raunak Kathuria
  • 3,185
  • 1
  • 18
  • 27
  • 1
    *"use event delegation rather than event handler"* Event delegation is binding an event handler to an ancestor of the element, instead of binding the handler directly to the element. In both cases you are working with event handlers. – Felix Kling Dec 17 '13 at 06:22
  • @FelixKling i meant to say direct event assignment, so should it be "rather than direct event assignment"? or how would you put it? Thanks for the info – Raunak Kathuria Dec 17 '13 at 06:27
  • Yep, that would be better! – Felix Kling Dec 17 '13 at 06:40
0

*If you are using jquery 1.7.2 *

  $("body").delegate("#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;
  }); 

 $("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;
  }); 

*If you are using jquery 1.10.2 *

    $(document).on("#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;
  }); 

   $(document).on("#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;
  }); 

   $(document).on(".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;
  });
Mahesh Reddy
  • 356
  • 4
  • 14