Binary Logic
Binary Logic

Reputation: 1547

Jquery: simple way to detach and re-atttach an element?

I know about detach() and the varios methods to insert html. But how do you detach an element, leave a placeholder, and then replace that placeholder with the element later on?

The only solution I can think of is to check for a prev(), then next(), then parent(). Store that placeholder, and then later on use that placeholder to put the element back.

There has to be a simpler way...

Thanks.

Upvotes: 1

Views: 2351

Answers (3)

Thierry Blais
Thierry Blais

Reputation: 2858

Use good 'ol $(elem).css('visibility', 'hidden'); to hide it while keeping it's place,

and then $(elem).replaceWith(newElem); to swap it out;

Upvotes: 1

thorn0
thorn0

Reputation: 10427

The replaceWith method is what you need, isn't it?

Upvotes: 1

writeToBhuwan
writeToBhuwan

Reputation: 3281

For attaching an element you must use .append() like this

 $('form').append("<div id='appended'>Hello World</div>");

For detaching an element you should use .remove() like this

 $('#appended').remove();

Upvotes: 0

Related Questions