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.
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.
$(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
You need to use event delegation:
$('#divMain').on('click', a, function(){
alert('hello');
});
Updated Fiddle: http://jsfiddle.net/W95wV/1/
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 ...
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/...
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'); ...