Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to replace an element including all the HTML tags

Supposing I have the correct location of an element whats the best way to replace all the html including the tags of a given block of HTML code using jQuery()? I'm looking to do something similar to the following. Is this a good way to do this? What other ways are available or useful to know about?

var location = 'td[id^="A0.R0.Work"]';
var element = jQuery(location).prev();
element.html('<h1>some code</h1>');

Thanks.

like image 877
Asher Avatar asked Feb 27 '26 01:02

Asher


2 Answers

Try .replaceWith()

$(element).replaceWith(other_stuff);
like image 102
SenorAmor Avatar answered Mar 01 '26 14:03

SenorAmor


The code you provide will try to assign the HTML to whatever jQuery's html function returns. Instead, pass your html as the argument to the html function:

var location = 'td[id^="A0.R0.Work"]';
var element = jQuery(location).prev();
element.html('<h1>some code</h1>');
like image 23
rjz Avatar answered Mar 01 '26 13:03

rjz