tags:

views:

128

answers:

4

I am skinning a 3rd party app that has no HTML natively. It all comes back from an onLoad event and a bunch of ajax calls.

I added jQuery to the page. I need to wrap() an element that is dynamically loaded. I can include a plugin if needed.

How do I do this? Thanks.

UPDATE: This works, but is there a better way?

$(document).ready(function() {

(function() {
  var length = $(".applicationShell").length;

  var h = setInterval(function () {
    if ($(".applicationShell").length > length) {
      length = $(".applicationShell").length;
      clearInterval(h);


      $(".applicationShell").addClass("test")

    }
  }, 100);
})();


});
A: 
$('#element-id').wrap( "<div class='wrapExample' />" );

I can give you more specific help if you post some code you need help with.

Helpful link?

BioBuckyBall
Does not work. The element is loaded dynamically after the ready block. Sorry.
Glen Lipka
@Glen Lipka Do this in the callback for your ajax calls, not the ready block.
BioBuckyBall
Unfortunately, the ajax calls aren't mine. I am skinning a third party app and don't have access to that part of it
Glen Lipka
@Glen Lipka added a link to a question asking for similar help. Let me know if that helps
BioBuckyBall
Thats the exact link (coincidentally) that I got the setInterval answer. I wish there was a more 'jQuery" like way to do it. I wonder why there isn't a DomChanged event.
Glen Lipka
@Glen Lipka Most likely a performance issue
BioBuckyBall
A: 

IF you want to do that upon another event, you could do:

$('.clickme').live('click', function() {
  $(myselector).wrap(/* my stuff to wrap */);// Live handler called.
});

where the "click" would be the event you wish to manage the behavior for and the .clickme is the class on the event instantiator.

Other option: do that wrap as part of the ajax success (making the assuption of jquery ajax here):

...snip
success: function(msg)
            {
                $(myselector).wrap(/* my stuff to wrap */);
            },
...

...

Mark Schultheiss
Sorry, this is no good either, as it depends on a click event. I want to wrap the element as soon as it shows up.
Glen Lipka
A: 

Edit: Unfortunately you can cannot use the livequery plugin to get the effect you're looking for. Thanks to @patrick for pointing this out. This fiddle shows it doesn't work with a native DOM element append ("Howdy" remains black). Uncomment the last line of the fiddle, and you'll see that when the jQuery append does finally run, livequery then "sees" the native DOM additions in addition to the jQuery (and both additions become green).

It's therefore likely you'll have to stick with a polling solution like the one in your question. The fact that jQuery sees non-jQuery DOM additions when it has cause to inspect the container perhaps suggests another way to poll -- just use livequery on the container as in the Fiddle, and then use setTimeout to perform a neutral jQuery operation (like removeClass('nothing')) on the container. Livequery would then "see" the new elements added between polls. This does work, although I'm not sure it's a better option.

Ken Redler
@Ken - I had the same answer, but I deleted it because I'm pretty sure `livequery` only works for elements added via jQuery. From the docs: *"If your plugin modifies the DOM without using the built-in DOM Modification methods (append, addClass, etc), you can register your plugin with Live Query like this..."*
patrick dw
@patrick -- you could be right. I'm trying a closer reading of the docs and taking a peek at the source.
Ken Redler
I tried this one and it didnt work unfortunately
Glen Lipka
A: 

Well, it seems I can't find a better solution. :( Here is the one I used.

$(document).ready(function() {

(function() {
  var length = $(".applicationShell").length;

  var h = setInterval(function () {
    if ($(".applicationShell").length > length) {
      length = $(".applicationShell").length;
      clearInterval(h);


      $(".applicationShell").addClass("test")

    }
  }, 100);
})();


});
Glen Lipka