jquery: binding events and .live

Posted on 2011-10-13
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 42

    Expert Comment

    by:Rob Jurd, EE MVE
    .live will add the event handlers to any new dynamically created content.

    have you read this:
    LVL 22

    Expert Comment

    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

    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
    LVL 81

    Expert Comment

    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 :"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

    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

    delegates appear to be the best solution here

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
    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.
    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…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now