tags:

views:

175

answers:

4

JQuery documentation has this to say about .load(fn) (http://docs.jquery.com/Events/load#fn ):

Note: load will work only if you set it before the element has completely loaded, if you set it after that nothing will happen.

So when one is supposed to bind the load event for <div id="test">? As far as I understand doing it before this div actually appears won't work, because $('#test') won't select it, and doing it after that is, as mentioned above, too late. Following code seems to support this:

<html>
<head></head>
<body>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;

<script type="text/javascript">

   $('#test').load(function(event) { event.target.css('color', 'blue'); alert('a');});

</script>

<div id="test">TEST</div>

<script type="text/javascript"> 
   $('#test').load(function(event) { event.target.css('color', 'red'); alert('b');});
</script>


</body>
</html>

After loading it nothing happens (no color changes, no alerts) and Firebug says everything is ok.

A: 

it may be firing before the DOM is ready, thus the #test div does not exist yet, try wrapping it in $(document).ready(...

should look something like:

$(document).ready(function(){
    $('#test').load(function(event) { event.target.css('color', 'blue'); alert('a');});
};)
Russ Bradberry
A: 

Only windows and images fire the load event, from what I can see.

$(function(){
  $("#test").css("color","blue");
});
Jonathan Sampson
Still nothing happens.
Olek
In this case, the load method isn't necessary from what I can tell.
Jonathan Sampson
+4  A: 

The load event was intended for elements that are loaded asynchronously, like images and ajax-loaded content.

It's useless for static content.

In your example, the first load() event handler is bound before the #test element is defined, so it won't work. The second load() event handler is too late, because #test is already loaded.

Philippe Leybaert
right-o. if you need to modify something when it loads, through it in `$(document).ready(...)`
geowa4
So is there any other way to force appearing static dom element and its dynamically created content simultaneously? I'am trying to avoid situation when after reloading the page user sees the static dom element with no content in it and it must take some time (like second or so) before this element is filled with result of javascript function.Of course I can create this initial content on the server side, but I was hoping this could be done purely in js.
Olek
A: 

If I understand your intentions correctly, the code you are intending to write is:

$(function() {
  $('#test').css('color', 'blue');
  alert('a');
});

If you would also like the #test element to perform a certain function when it's content is loaded via an AJAX call, try this:

$(function() {
  $('#test').css('color', 'blue').load(function() {
    $('#test').css('color', 'red');
    alert('b');
  });
  alert('a');
});
Marve
Actually, I was trying to do something different and in the way that now seems strange to me. Not to mention it doesn't work. :) I explained it under Philippe Leybaert's answer.
Olek
@Olek Thanks for the clarification. I understand the issue with the delay in loading the content. You have almost certainly thought of this already, but perhaps a "loading" dialog or indicator would suffice?
Marve