How to remove an element and clear the total window using JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to remove an element and clear the total window using JavaScript?

I use, getElementById() and removeChild() method for removing. But if I create more than one specific element with same id using JavaScript, then removeChild() removes only one block. I mean if I create 3 same elements with same id, how I remove all of these with one-click, like I refresh the page? removeChild() removes only one element with one-click.

16th May 2019, 3:58 AM
Pranab Ranjan Mandal
Pranab Ranjan Mandal - avatar
8 Answers
+ 3
Then for repeated elements, you should use class, not id.
16th May 2019, 6:33 AM
Calviղ
Calviղ - avatar
+ 3
Here a sample to illustrate button click remove all same class elements. https://code.sololearn.com/WX4mCJGyxEld/?ref=app
16th May 2019, 6:35 AM
Calviղ
Calviղ - avatar
+ 2
Same id only allow to use once.
16th May 2019, 4:41 AM
Calviղ
Calviղ - avatar
+ 2
First, for targeting more elements at once use class instead if id.
16th May 2019, 5:57 AM
Maneren
Maneren - avatar
+ 1
Here is my code! Give some inputs and click the 'ok' button 2 or 3 times, and then click the 'reset' button...This is my problem. And the problem still persists. Calviղ TheCoder | Yep, that's me Maneren thanks friends, for helping me 😊😊😊 https://code.sololearn.com/WOb0w15Qcksb/?ref=app
16th May 2019, 7:57 AM
Pranab Ranjan Mandal
Pranab Ranjan Mandal - avatar
0
TheCoder | Yep, that's me Thanks for helping me ☺️. But sorry to say that, it doesn't solve my problem. I did exactly what they told to do. I think you can't understand my question or you can elaborate me with more simplicity! Please, pardon me.
16th May 2019, 4:19 AM
Pranab Ranjan Mandal
Pranab Ranjan Mandal - avatar
0
Calviղ I have a form and when I clicked 'submit', it gives me a result within a html element, which is created by JS (by createElement() method with an id). If clicked 'submit' 3 times, it gives me same result 3 times and with the same id, because it is made by the same createElement() method. But, I want to remove id as well as the element with just one-click. I have to click 'reset' button 3 times to get a fully refreshed page (here I use removeChild() method in the reset function)
16th May 2019, 4:56 AM
Pranab Ranjan Mandal
Pranab Ranjan Mandal - avatar
0
I used your method but it gives me error!Maneren
16th May 2019, 7:03 AM
Pranab Ranjan Mandal
Pranab Ranjan Mandal - avatar