Marc
Marc

Reputation: 6771

jQuery get result of .insertBefore

My Task:

Inserting the Element is easy with the .insertBefore method:

var result = $('#myInsertPosition').insertBefore('<div>newInsert</div>');

That gives us a Dom like this:

<div>newInsert</div>
<div id="myInsertPosition"></div>

But result in this case is still the #myInsert Div but I need the newInsert Div.

How can I get the new inserted Div as jQuery object?

Thoughts are with .prev() or other selectors but I can't find a reliable solution.

Upvotes: 0

Views: 239

Answers (1)

omma2289
omma2289

Reputation: 54629

insertBefore() does return the newly inserted jquery object but your call is backwards, you want to do:

var result = $('<div>newInsert</div>').insertBefore('#myInsertPosition');

Upvotes: 2

Related Questions