Shebas Khan Shebas Khan - 3 months ago 5
Javascript Question

I want to hide the button in javascript

I have a HTML form code is as follows

<div id="foo" style="display:none;">
<input type="text" name="name">
<input type="text" name="name1">
<input type="text" name="name2">
</div>
<input type="button" onclick="toggle_visibility('foo');"
value="Add more" />


Upon clicking add more all the above textboxes, but the button is still visible and i want to hide that button

Following is the javascript

function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}

Answer

function toggle_visibility(btn,id) {
       //hide button
       btn.style.display = 'none';
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
<div id="foo" style="display:none;">
   <input type="text" name="name">
   <input type="text" name="name1">
   <input type="text" name="name2">
</div>
   <input type="button" onclick="toggle_visibility(this,'foo');" 
   value="Add more" />

Try this. Don't forget to mark as answer.

Comments