jquery: binding events and .live

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
AidenAAsked:
Who is Participating?
 
AidenAConnect With a Mentor Author Commented:
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?
0
 
RobOwner (Aidellio)Commented:
.live will add the event handlers to any new dynamically created content.

have you read this: http://api.jquery.com/live/
0
 
MrunalCommented:
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
AidenAAuthor Commented:
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

http://stackoverflow.com/questions/4579117/jquery-live-vs-delegate
0
 
leakim971PluritechnicianCommented:
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() {


0
 
AidenAAuthor Commented:
delegates appear to be the best solution here
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.