Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace whole element rather than innerHTML?

I can do this:

$('my-panel').innerHTML = '<p> New content </p>';

But if there any way of doing something like

$('my-panel').wholeHTML = "<div id='my-panel'><p> New Content</p></div>";

I can't find any way. If I can't do something like this, I'll have to refactor a whole bunch of stuff, which would be time consuming.

like image 812
Oliver Avatar asked Aug 05 '11 19:08

Oliver


People also ask

Should I avoid innerHTML?

The use of innerHTML creates a potential security risk for your website. Malicious users can use cross-site scripting (XSS) to add malicious client-side scripts that steal private user information stored in session cookies.

How do I remove an innerHTML element?

innerHTML property innerHTML property will get or set the HTML markup contained within the element. But you can utilize this property to “remove” any elements within the container, by setting the value to an empty string. This property is fully cross-browser, read full docs on innerHTML.


2 Answers

What about outerHTML, which includes the 'whole' tag:

$('my-panel').outerHTML = '<p> New content </p>';

http://jsfiddle.net/pimvdb/Sah2U/1/

like image 71
pimvdb Avatar answered Sep 29 '22 03:09

pimvdb


You use mootools, right? you could easily replace the element, i.e.:

Elements.from("<div id='my-panel'><p> New Content</p></div>").replaces($("my-panel"));
like image 30
stecb Avatar answered Sep 29 '22 02:09

stecb