I have a table element <table id="my-table"></table>
. I want to replace the selected table with a <div class="some-class></div>
. What is the fastest way to achieve this? I want to use pure JavaScript only.
Asked
Active
Viewed 85 times
1

Rifat Bin Reza
- 2,601
- 2
- 14
- 29
-
2What have you tried so far? – Daweed Mar 04 '21 at 08:02
-
Not sure if the fastest but the most easy - document.querySelector('#my-table').outerHTML = document.querySelector('some-class'); – Lakshya Thakur Mar 04 '21 at 08:03
-
@Daweed I cannot think of a safer way to replace the selected element itself. I've read it's not safe to replace the html directly – Rifat Bin Reza Mar 04 '21 at 08:07
-
1Duplicate: [How to replace DOM element in place using Javascript?](https://stackoverflow.com/questions/843680/how-to-replace-dom-element-in-place-using-javascript) – Mar 04 '21 at 08:08
-
@ChrisG - I'm an idiot. Of **course** this is asked and answered. – T.J. Crowder Mar 04 '21 at 08:10
-
1*"I want to use pure JavaScript only."* JavaScript is a *language*, not an API (though like all languages it has a standard library -- which doesn't know anything about browsers). The native browser API you're looking for is the [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model). – T.J. Crowder Mar 04 '21 at 08:12
-
Got it, thanks all – Rifat Bin Reza Mar 04 '21 at 08:14
-
1@RifatBinReza - FWIW, I posted the answer I originally posted below [here](https://stackoverflow.com/a/66470963/157247). – T.J. Crowder Mar 04 '21 at 08:16