Monday 27 April 2009

jQuery events not firing for dynamic content

When adding dynamic content (using AJAX or DOM modification) using jQuery, events are not fired for the new objects, for example if you have the following html:

<button id="load" href="#">Test</button>
<div id="container"></div>


When I click the load button I want to add a link to another page inside the container DIV, so I use the following jQuery.


$(function() {
$("#load").click(function() {
$("#container").html("<a id='popup' href='contactForm.html'>popup</a>")
});

$("#popup").click(function(){
alert("Clicked");
return false;
});
});


The click event for the dynamically created anchor tag (id="popup") will not fire using the above because this code is run once the document is ready, and at that time jQuery will look for an object with an Id of popup which doesn't exist yet.

I've found a few different ways to do this:

1. Add a generic click event for the body tag and check what was clicked, for example:


$("body").click(function(e){
var target = $(e.target);
if (target.is("#popup")) {
alert("Clicked");
return false;
}
});


Using this example you check what was clicked using target.is(), you can use any valid CSS selector statements to check the item clicked. This way is simple to implement but can quickly grow if you have a lot of dynamic content.

2. Use the live command, which will tell jQuery to bind any current and future references of "#popup" to the click event.



$("#popup").live("click",function(){
alert("Clicked");
return false;
});



The following 3 ways are variations on the same theme in that the events are bound after content creation.

3. Use the jQuery Bind command to bind the click event to a particular function on creation. e.g.:


$("#load").click(function(){
$("#container").html("<a id='popup' href='contactForm.html'>popup</a>").bind('click', loadClick);
//can also use
//$("#container").html("<a id='popup' href='contactForm.html'>popup</a>").click(loadClick);
});

function loadClick(){
alert("Clicked");
return false;
}


4. Insert the function for handling the click event within the initial click event when you create the popup link. This runs the event binding as soon as the item is added.


$("#load").click(function(){
$("#container").html("<a id='popup' href='contactForm.html'>popup</a>");
$("#popup").click(function(){
alert("Clicked");
return false;
});
});


5. Similar to the above method except this way you create a method which does all data binding (bindEvents()). This can be useful if you have a lot dynamic content that needs to be bound at the same time, it can be encapsulated into 1 event.


$("#load").click(function(){
$("#container").html("<a id='popup' href='contactForm.html'>popup</a>");
bindEvents();
});

function bindEvents(){
$("#popup").click(function(){
alert("Clicked");
return false;
});
};



There's also a plugin called livequery that makes binding easier.

7 comments:

corey said...

thanks! great post! I'm new on JQuery Development, and i was with this problem at work about 4 days ago!
Thanks again

From Guatemala City

Abhishek said...

Thank you so much!! Seriously!

Andres said...

Great post it saved my life I didn't realized why this problem was happening until I got here. Thanks a lot good karma!

Anonymous said...

http://antina.3dn.ru/ тут делают деньги

Anonymous said...

Thank you so much for the great help

lulur bali murah said...

salam kenal bos. lagi jalan jalan pagi nih

Unknown said...

哪些男性是陽痿的高發人群犀利士cialis
由犀利士cialis报道:哪些男性是陽痿的高發人群?犀利士 威而鋼 威而鋼專賣店 犀利士 犀利士 陽痿作為男科疾病的老大,陽痿的發生不是偶然的,調查發現,陽痿“愛上”的男性一般都具有或多或少的相似性。 對此,你有所了解嗎? 如果不清楚,下面跟隨我來了解一下。
對比看看,你是陽痿的“最愛”嗎?
1、陽痿“最愛”之健康狀況較差者
如果有心髒病、高血壓、糖尿病、高膽固醇血症、抑鬱症、犀利士 犀利士專賣 犀利士哪裡買 犀利士5mg價格 壯陽藥品 犀利士專賣 威而鋼 壯陽藥 威而鋼專賣店 犀利士哪裡買腎衰竭、前列腺手術史、盆腔內手術史、脊髓損傷多發性硬化、內分泌疾病、精神疾患(如精神分裂、抑鬱症等)長期服用降壓藥、降糖藥、抗抑鬱藥、抗精神病藥物、鎮靜等及興奮藥。
2、陽痿“最愛”之不良的生活習慣者
這些年來,陽痿的患者年齡越來越小,這與年輕人的生活習慣分不開。 在25-35歲的男性患者身上,往往具有較長的吸煙史且吸煙數量大、酗酒、濫用藥物、工作緊張、生活壓力大、威而鋼哪裡買 犀利士哪裡買 壯陽藥品 壯陽藥 威而鋼哪裡買 犀利士專賣 威而鋼 威而鋼哪裡買 威而鋼專賣店 威而鋼藥局長距離騎車等,均是陽痿大大愛。
3、陽痿“最愛”之年長者
年齡在40歲以上的男子,隨著機體逐漸地衰退,會出現或輕或重的性功能減退或異常,這是一個普遍的問題,也是一種無法控制的趨勢。 所以,要在醫生的幫之下最大限度的解決好這個問題。

Free Hit Counter