JavaScript Click not working after clearing and appending the html click not working after append,click not working after ajax,

click not working after i clear and append the html inside a div using jquery. Here is the html code

<div id="divMain">
</div>
<input id="btn" type="button" value="Clear&Add"/>

Here is the jQuery code

var a = $('<a/>').attr({'id':'aH','href':'#'}).text('Hello');
a.click(function(){
    alert('hello');
});
$('#divMain').append(a);



$('#btn').click(function(){
   var newA = $('#aH');
    $('#divMain').html('');
    $('#divMain').append(newA);
});

Here is jsfiddle

Simple click on the alert link in fiddle , it shows an alert.Now click on the Clear&Add button .And now click on alert.It doesn't work.

Answer:1

You need event delegation to bind the event with dynamically added elements. You also need to create elemet with id aH as you have removed the element from DOM without preserving it.

Live Demo

$(document).on('click', '#aH', function(){
    alert('hello');
});

You can try adding the globally created a and you would not need to bind click again.

$('#divMain').append(a);

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers, jQuery api

Answer:2

You need to use event delegation:

$('#divMain').on('click', a, function(){
    alert('hello');
});

Updated Fiddle: http://jsfiddle.net/W95wV/1/

Answer:3

I have nested forms in an ng-repeat with a button whose click event calls a function that does a couple of things including a $scope.$broadcast. Everything in the function triggers once except for the ...

I have nested forms in an ng-repeat with a button whose click event calls a function that does a couple of things including a $scope.$broadcast. Everything in the function triggers once except for the ...

  1. angular broadcast called twice

I am using the following function(which i got from web) to resize the column in kendo ui. this is based on Index, i am looking out to see if there can be an option to select by column title or field/...

I am using the following function(which i got from web) to resize the column in kendo ui. this is based on Index, i am looking out to see if there can be an option to select by column title or field/...

  1. kendo resize columns
  2. kendo column resize event
  3. kendo autofit column
  4. kendo grid resize column
  5. kendo grid resize column programmatically
  6. kendo grid resize column angular
  7. kendo grid resize column width
  8. kendo grid resize column jquery
  9. kendo grid resize column event
  10. kendo grid resize column mvc
  11. kendo treelist resize column
  12. kendo ui resize columns
  13. kendo grid resize columns javascript
  14. kendo grid column resize not working
  15. kendo ui grid resize columns
  16. kendo grid column resize not working in chrome
  17. kendo grid auto resize column
  18. kendo grid column resize handle width
  19. kendo grid column resize false
  20. kendo grid cannot resize columns

I need to convert session.lastAccessedTime object from jsp into Javascript Date object. Currently, it displays as long object. How can I convert to Javascript date object? console.log('...

I need to convert session.lastAccessedTime object from jsp into Javascript Date object. Currently, it displays as long object. How can I convert to Javascript date object? console.log('...

I'm trying to dubug next JavaScript code in on my HTML page: var langs = document.getElementsByClassName('ss-code-language'); for (var i in langs) { langs[i].classList.remove('ss-highlighted'); ...

I'm trying to dubug next JavaScript code in on my HTML page: var langs = document.getElementsByClassName('ss-code-language'); for (var i in langs) { langs[i].classList.remove('ss-highlighted'); ...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. canada vaccines
  6. canada goose
  7. cane corso
  8. canes
  9. canadian prime minister
  10. cancun flights
  11. canker sore
  12. candy
  13. cancer
  14. cancun
  15. canon
  16. canvas prints
  17. cancer sign
  18. candace owens
  19. canada news
  20. candytopia