views:

232

answers:

2

Here I'm wrapping an HTML tag with another tag '$holder' below.

  $holder = $('<div />')
    .addClass('joverlaytext')
    .css({
        position:'relative', 
    });
  $(this).wrap($holder);

Now after this statment how to get the object/reference to this newly created HTML element, ie. jQuery obj of '$holder'

+2  A: 

Just continue the chain, e.g.:

$(this).wrap($holder).show();

If you look at the manipulation methods documentation, .wrap(elem) returns a jQuery object, so chaining or

var result = $(this).wrap($holder);

will work.

Nick Craver
A: 

Just continue to use $holder as normal, placing it in a wrap function will not change it's jQuery object reference. So you could do this.

  $holder = $('<div />')
    .addClass('joverlaytext')
    .css({
        position:'relative', 
    });
  $(this).wrap($holder);
  $holder.show().doTheFunkyChickenDance();

Also as always, try not to use $(this), instead cache the object lookup by doing this

var $this = $(this);
// then just use $this as normal so
$this.wrap($holder);

Using the var keyword is also important, as it creates a locale variable rather than a global variable which should be avoided to stop banging heads on walls later down the track.

balupton