Hey. It was the task to write a script, when you click on the button it will be easy to hide.

Here is the button:

<button name="OK" onclick="my_onclick()" id="button_2" >OK</button> 

Here is the function:

  function my_onclick() { var element = document.getElementById('button_2'); element.style.display = "none"; } 

C javascript encountered 1 time. The function works, but I understand that it is, let's say, not universal and works only with button_2. How should I correctly set the function parameter getElementById('') to work for all buttons with different IDs? Thank!

  • You can by "name" - cas
  • document.getElementById (name); changed but no longer works ... missed something? - sonicsonic1
  • one
    Something like this: <button onclick = "my_onclick (this)"> OK </ button> <button onclick = "my_onclick (this)"> OK2 </ button> function my_onclick (target) {target.style.display = " none "; }; - ReinRaus
  • ReinRaus, THANK YOU VERY MUCH! - sonicsonic1

2 answers 2

http://jsfiddle.net/PYjhS/

 <button name="OK" onclick="my_onclick('button_2')" id="button_2" >OK</button> <button name="OK" onclick="my_onclick('button_3')" id="button_3" >OK</button> function my_onclick(el) { document.getElementById(el).style.display = "none"; } 
  • Thanks for the prompt assistance! - sonicsonic1

 document.addEventListener('click', function (event) { if (event.target.tagName === 'BUTTON') { event.target.style.display = 'none'; } }); 
 <button>1</button> <button>2</button> <button>3</button> <button>4</button> <button>5</button> <button>6</button> <button>7</button> <button>8</button>