Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery button not respond to click method

Tags:

jquery

click

so i am dynamically rendering a paragraph with jquery using the append method and i want to add a click event to it but for some reason the click event is not working, i know the solution is probably simple but I am new to jquery and would appreciate any help...I know the code inside the function works because i tested it with a static button, it is just not working with the dynamic one..Thanks in advance for any help,

here is my code

$(this).parent().parent().children("div").append("<p class='tryAgain'>Try Again</p>");

the click function code,

$(".tryAgain").click(function() {......}
like image 296
skevthedev Avatar asked Jan 18 '23 06:01

skevthedev


1 Answers

Anything you add to the DOM after the document.ready has fired needs to use .live or .delegate in order to add an event handler to the newly added element.

For instance:

$('.tryAgain').live("click", function() {...});

If you are using jquery 1.7+ you should use .on:

$(document).on("click", ".tryAgain", function(){ ... });
like image 177
amurra Avatar answered Jan 29 '23 07:01

amurra