Monday, April 25, 2011

Does load() retrieve an element with or without its descendants?

I made some tests with the load() command of jQuery, something like: $('').load('my.html div#content', function(){ etc. I was surprised to see that I was able to retrieve the div#content but not its child elements. When I substituted div#content by any of the child descriptors, e.g. a.opts I got the expected result - in this case, all the .opts links. However I have many different kind of elements inside my div#content; shoud I call them one by one, with a new load() statement, or am I doing something wrong?

From stackoverflow
  • load() will get the element you specify and all its child elements. It could be there's something wrong with the selector you're using. Could you post some code?

    EDIT: I'm guessing the selectors you're using are to blame. I'm not sure exactly what the issue is, but you might want to try using just the element ids in the selectors (make sure the elements you're using have unique ids).

    Here's how I've used this in the past:

    $('#targetDiv').load('http://localhost/test.aspx #sourceDiv');
    
    Kevin Tighe : Hmm. When I've used load() I specified the target element in the $() call like so: $('.maincontent').load(...). Maybe you could try this instead of using the .appendTo call?
    Kevin Tighe : Glad to hear it :)

0 comments:

Post a Comment