00 Votes

jQuery: Replace element

Question by Compi | 2012-07-12 at 19:12

I would like to replace a full element of my HTML code with jQuery.

How to exchange the contents of an element, I already know:

var newcontent = 'New Content';
$('#element').empty().append(newcontent);

However, I want, that the entire element with a specific id will be replaced. So, the old element should disappear completely the new one should appear at this point. Is there a way?

ReplyPositiveNegative
0Best Answer0 Votes

Yes you can.

Just try using the following code:

$('#element').replaceWith('<div>New Element!</div>');

With this, you swap the old element with the id "element" to the new element (the HTML code in brackets) completely.
2012-07-13 at 19:27

ReplyPositive Negative
Reply

Related Topics

jQuery: Show and hide elements

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.