views:

127

answers:

4

Is there a jQuery equivalent to do the following:

$(document).ready(function() {

for an element:

$(a).ready(function() {

I have content in an updatepanel and I am calling jQuery UI's .button() on some anchor elements. After the updatepanel refreshed, the anchors are rerendered and lose the UI styling.

I already know how to detect the end of an ajax request using .NET AJAX's add_endrequest(handler), but was hoping for a neater solution using jQuery.delegate.

e.g.

$('body').delegate('#mybutton', 'load', (function(){  //this doesnt work... }
+2  A: 

you can easily track on load for any element in the web page.


$(function(){$("#ele_id").bind("load", function () { alert('hurray!') });})

for more details see this thread : http://stackoverflow.com/questions/1383870/jquery-how-do-you-get-an-image-to-fade-in-on-load

lakhlaniprashant.blogspot.com
actually, the load event only applies to a few elements such as `img`, `object`, `script` and `window`.
Andy E
I don't believe it works on any element, only elements that load content (img, iframe)
Jamie Carruthers
I understand that, I think the real solution is, how to find something like UpdatePanleRendered in js microsoft ajax javascript framework
lakhlaniprashant.blogspot.com
See my answer for pageLoad
Mark Schultheiss
+1  A: 

elementReady: a jQuery plugin

Branimir
+1  A: 

If I understand your requirement correctly, one way to do this is with the Live Query plug-in.

Live Query ... has the ability to fire a function (callback) when it matches a new element and another function (callback) for when an element is no longer matched

For example:

$('#someRegion a').livequery( function(){ 
  do_something();
});


Update: Since the DOM changes are not running through jQuery, unfortunately livequery doesn't see them. I mulled over this issue before and considered a polling-based solution in this answer.
Update: Polling is kind of ugly, but if there's really no other alternative, here's a polling-based solution that uses a jQuery "dummy" manipulation to make livequery "see" the change. You'd only want to consider something like this as a last resort -- if there's no option to tie into a callback method.

First, set up livequery watching the container where the updates will occur:

$('div#container').livequery( function(){ 
  $(this).css('color','red'); // do something
});

And then use setInterval(), here wrapped in a convenience function:

function pollUpdate( $el, freq ){
  setInterval( function(){
    $el.toggleClass('dummy');
  }, freq); 
};

So you can have:

$(document).ready( function(){
  pollUpdate( $('div#container'), 500 );
});

Here's a working example. Click the button to add new DOM elements without jQuery, and you'll see they get picked up (eventually) and restyled by livequery. Not pretty, but it does work.

Ken Redler
I can get livequery firing if I add a new element using jquery.append() however it doesn't detect the new elements added to the page when the updatepanel is refreshed.
Jamie Carruthers
I apologize -- I see now that the changes are not being made by jQuery. Live Query thus doesn't see them. See the edit above.
Ken Redler
A: 

Are you are trying to solve the wrong problem? IF it is just style: Try to add instead, css in the header so that the update does not impact the style. Note: Not exactly sure on your selectors based on your question for these examples.

$('head').append('<style type="text/css">body a{margin:0;}</style>'); 

OR

$('<style type="text/css">body a {margin: 0;}</style>').appendTo($('head'));

If you indeed need some event management you should be able to use the delegate using context. see notes here: http://brandonaaron.net/blog/2010/03/4/event-delegation-with-jquery

$('body div').delegate('a', 'change', function(event) {
    // this == a element
    $(this).addClass(class here);
});

or with a chain

$('body').children('div').delegate('a', 'change', function(event) {
    // this == a element
  $(this).addClass(class here);
});

Now for the real answer, since you have access to the event you can add it directly to your page with the pageLoad function; Add this:

function pageLoad(sender, args) 
{ 
   if (args.get_isPartialLoad()) 
   { 
       //jQuery code for partial postbacks can go in here. 
   } 
} 
Mark Schultheiss
more stuff on update panel here: http://stackoverflow.com/questions/256195/jquery-document-ready-and-updatepanels
Mark Schultheiss
@Mark, JQuery UI's .button() method doesn't just add css styling. It also adds behaviour to the wrapped set of elements to which it is applied.
Daniel Dyson
@Daniel Dyson - Indeed, thus if the .delegate does not work for the load event, then the pageLoad should work to apply the .button with a capture of the load event.
Mark Schultheiss
@Mark - yes, my comment was just regarding the first paragraph of your answer.
Daniel Dyson