-1

I have created a button dynamically in JavaScript and i am deleting the same when user clicks X symbol on it.. I am setting style.display=none to achieve this but the problem is in HTML there is space in it even after deleting the button. How can i delete the button without any space in HTML.

 var x = document.getElementById(id);
 x.style.display='none';
user2319726
  • 143
  • 1
  • 1
  • 10

2 Answers2

0

First of all, if you Google remove HTML element in javascript the first thing you see is this: How to remove an HTML element using Javascript?

and it works, but I'll pitch in my 2 cents anyways since that one uses parentElement, which is kinda outdated.

There's a .remove() method in javascript that lets you remove an html element. It's as simple as:

var x  = document.querySelector("#id");
x.remove();

but I doubt that will solve your issue, you might want to remove the parent element if it has a margin or padding like so:

var x  = document.querySelector("#id");
x.parentElement.remove();

cheers

Alek Angelov
  • 111
  • 6
-1

try it if u want to change the css

<script type="text/javascript">
    $("#id").css('display', 'none');
</script>

or use it for add or remove the class. change the add to remove if want remove the class

<script type="text/javascript">
    document.getElementById("submit").classList.add('disabled'); 
</script>