0

Hi I am trying to get a few buttons to work but all the other things I found didnt work here it is

document.getElementById("Element").remove();

isnt working

<button class="btn btn-danger" onclick="reset()">Reset</button>

Error:

script.js:30 Uncaught TypeError: Cannot read property 'remove' of null
at reset (script.js:30)
at HTMLButtonElement.onclick (index.html:14)

Im sorry if this is formatted weird but I am new to this.

Lumic
  • 39
  • 3
  • 8
  • 3
    Does the element to be removed have `id="Element"`? – Ben Jul 30 '20 at 06:15
  • 1
    The button doesn't have any id. You need to give it the correct id to be able to remove it. – Geshode Jul 30 '20 at 06:16
  • Can you post full code with elements having id `Element` – Sivakumar Tadisetti Jul 30 '20 at 06:17
  • Welcome to Stack Overflow! Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output using the `[<>]` snippet editor. – mplungjan Jul 30 '20 at 06:18
  • 1
    If you need to remove the button with id "Element" then the button need to have a id="Element " attribute, but the example given doesn't have the id attribute. – krishna Jul 30 '20 at 06:20
  • 3
    Does this answer your question? [How to remove an HTML element using Javascript?](https://stackoverflow.com/questions/5933157/how-to-remove-an-html-element-using-javascript) – Umair Khan Jul 30 '20 at 06:21

4 Answers4

2

Try this

<button id="reset-button" class="btn btn-danger" onclick="reset()">Reset</button>

And you can remove the button via

document.getElementById("reset-button").remove();

Vasanth Gopal
  • 1,215
  • 10
  • 11
1

The element you want to remove needs to have id="Element".

For example if you want to remove a div it would look like:

<div id="Element"> Div to remove </div>

then your script:

function reset() {
  document.getElementById("Element").remove();
}

will work for the button:

<button class="btn btn-danger" onclick="reset()">Reset</button>
Ben
  • 2,348
  • 1
  • 20
  • 23
1

I didn't get what you are trying to do, I am assuming you are trying to remove the button.

you are trying to get button by element id but there is no id attribute in the button tag, it should look like something like below

document.getElementById("id_name").remove();

<button id="id_name" class="btn btn-danger" onclick="reset()">Reset</button>
0

Just give the id name to your button with the same name you're giving in remove.

document.getElementById("Element").remove();
<button id="Element" class="btn btn-danger" onclick="reset()">Reset</button>
Nidhi Dadiya
  • 798
  • 12
  • 31