jquery: binding events and .live

Posted on 2011-10-13
Medium Priority
Last Modified: 2012-05-12
Hi, I was trying to register events using jquery and ran into the problem that they just aren't called. Well some are and some aren't. I think the events that do not get called might have something to do with them being in an ajax update panel and so they are loaded dynamically and for some reason this causes a problem. maybe.

Either way, using .live solves the problem e.g. $("#myradiobutton").live("change", function() {}

So, reading a bit about this I'm coming across a lot of confusing stuff suggesting .live is a bad thing but can't see anything which explains this easily and simply and what I should or shouldn't be doing here.

Can anyone just clear this up for me?

Thanks, Aiden
Question by:AidenA
LVL 43

Expert Comment

ID: 36966607
.live will add the event handlers to any new dynamically created content.

have you read this: http://api.jquery.com/live/
LVL 22

Expert Comment

ID: 36966928
Also you can try this, in case of update panel existence on aspx page:

write this in script tag. this is javascript function.

function pageLoad(sender, args) {
            if (args.get_isPartialLoad()) {
                // called on each partial postback
            else {
                // same as not ispostback page
                // Only first time will be called

Hope this helps you.

Author Comment

ID: 36967253
Hi thanks but i guess that doesn't really answer my question...

this link seems to basically show why there is a problem using live though and what should be used instead

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 83

Expert Comment

ID: 36967748
live use lot of ressources because it check if the events are for your element at the document level
for example you click anywhere on the page, live need to check if it's not on your element

with delagate, it's not somewhere on the page but somewhere in a container, for example somewhere in a div or a table

if possible use bind instead each time your page is updated
in an ajax call downloading html content

instead :
element.live("click",function() {
$.ajax({ url:"loadcontent", success:function(html) { $("#div").append(html)

use :
$.ajax({ url:"loadcontent", success:function(html) { $("#div").append(html); element.bind("click",function() {


Accepted Solution

AidenA earned 0 total points
ID: 36967759
well, didn't know exactly what to use so just use delegates on everything linking to the parent layout div like below

$("#leftcol").delegate("#txtCustomerName", "keyup", function(){

is that ok?

Author Closing Comment

ID: 37301584
delegates appear to be the best solution here

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question