Ben
Ben

Reputation: 1022

Make a reference to an element created via the jquery wrapInner function

I'm wrapping some elements using the jQuery wrapInner function. Like this.

$( "#container" ).wrapInner( "<div></div>" );

But I want to know the best way make a reference to this newly created div. I was hoping I could just do

var featuresWrap = $( "#container" ).wrapInner( "<div></div>" );

But this just creates a reference to "#container". So is this best way just to do the following?

$( "#container" ).wrapInner( "<div></div>" );
var featuresWrap = $( "#container div" );

Thanks

Upvotes: 0

Views: 240

Answers (2)

Joel Cornett
Joel Cornett

Reputation: 24788

Because wrapInner causes the element to have one immediate child, you can simply do:

var featuresWrap = $("#container").wrapInner("<div>").children();

to get the jQuery object associated with the created div.

In order to get the associated HTMLElement, simply do:

var htmlElement = featuresWrap.get(0);

Upvotes: 4

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

like this:

var featuresWrap = $( "#container > div" );

Upvotes: 1

Related Questions