Using jQuery to replace an element including all HTML tags

Let's assume I have the correct position of the element. What's the way to replace all the html including the tags of a given HTML block using jQuery ()? I want to do something similar to the following. Is this a good way to do it? What other methods are available or helpful to review?

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

      

Thank.

+3


source to share


3 answers


JQUERY is sometimes too much overhead.



var location = document.getElementById('A0.R0.Work');
location.innerHTML = '<h1>some code</h1>';

      

0


source


Try .replaceWith()



$(element).replaceWith(other_stuff);

      

+2


source


The code you provided will try to assign HTML to the jQuery function html

. Instead, pass your html as a function argument html

:

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

      

+1


source







All Articles