Making a button invisible by clicking another button in HTML
Asked Answered
U

12

9

How can I make a button invisible by clicking another button in HTML?
I have written like below, but it doesn't work.

<input type="button" onclick="demoShow();" value="edit" />
<script type="text/javascript"> 
    function demoShow()
    { document.getElementsByName('p2').style.visibility="hidden"; }
</script>
<input id="p2" type="submit" value="submit" name="submit" />
Uproarious answered 19/12, 2011 at 4:33 Comment(0)
A
14

write this

To hide

{document.getElementById("p2").style.display="none";}

to show

{document.getElementById("p2").style.display="block";}
Achaea answered 19/12, 2011 at 5:30 Comment(0)
W
5

For Visible:

document.getElementById("test").style.visibility="visible";

For Invisible:

document.getElementById("test").style.visibility="hidden";
Wittol answered 24/2, 2014 at 6:38 Comment(0)
D
2

Use the id of the element to do the same.

document.getElementById(id).style.visibility = 'hidden';
Dejected answered 19/12, 2011 at 4:37 Comment(0)
C
2
  1. getElementById returns a single object for which you can specify the style.So, the above explanation is correct.

  2. getElementsByTagName returns multiple objects(array of objects and properties) for which we cannot apply the style directly.

Chace answered 19/12, 2011 at 5:42 Comment(0)
C
1

try this

function demoShow() {   
document.getElementById("but1").style.display="none";

}



<input type="button" value="click me" onclick="demoShow()" id="but" />

<input type="button" value="hide" id="but1" />
Cultrate answered 19/12, 2011 at 4:44 Comment(2)
if you want occupy some space in the sense apply "visibility:hidden", otherwise "display:none;"Cultrate
I was just about to suggest that you explained the difference between the two lolParity
I
1

Using jQuery!

var demoShow = function(){
    $("#p2").hide();
}

But I would recommend you give an id to your button on which you want an action to happen. For example:

<input type="button" id="p1" value="edit" />
<input type="button" id="p2" value="submit" name="submit" />

<script type="text/javascript"> 
$("#p1").click(function(){
    $("#p2").hide();
});
</script>

To show it again you can simply write: $("#p2").show();

Indemnification answered 22/5, 2014 at 8:13 Comment(0)
P
0

To get an element by its ID, use this:

document.getElementById("p2")

Instead of:

document.getElementsByName("p2")

So the final product would be:

document.getElementsById("p2").style.visibility = "hidden";
Parity answered 19/12, 2011 at 4:38 Comment(0)
A
0

Use this code :

<input type="button" onclick="demoShow()" value="edit" />
<script type="text/javascript"> 
function demoShow()
{document.getElementById("p2").style.visibility="hidden";}
</script>
<input id="p2" type="submit" value="submit" name="submit" />
Attenweiler answered 23/9, 2013 at 6:43 Comment(0)
T
0
$( "#btn1" ).click(function() {
 $('#btn2').css('display','none');
});
Tenedos answered 6/6, 2014 at 12:8 Comment(1)
This answer need more than a block of code. For one thing, it depends on another third party library: jQuery. A better answer describes what the code does and why the particular approach was chosen.Misusage
F
0

Try this

<input type="button" onclick="demoShow()" value="edit" />
<script type="text/javascript"> 
function demoShow()
{p2.style.visibility="hidden";}
</script>
<input id="p2" type="submit" value="submit" name="submit" />

http://jsbin.com/gurolawu/1/

Fluoresce answered 7/6, 2014 at 12:46 Comment(0)
T
0

I found problems with the elements being moved around using some of the above, so if you have objects next to each other that you want to just swap this worked best for me

document.getElementById('uncheckAll').hidden = false;
document.getElementById('checkAll').hidden = true;
Ticker answered 28/2, 2019 at 10:31 Comment(0)
S
0

I did that way

<script>
            const AddMoreBtn = document.getElementById('add-more')
            AddMoreBtn.onclick = function(){
                if (document.getElementById('extraField').style.visibility == 'collapse'){
                    alert('Extra fields are not obligatory to be filled')             
                    document.getElementById('extraField').style.visibility='visible';
                } else{
                    document.getElementById('extraField').style.visibility='collapse';
                }
            }
</script>
Sarabia answered 27/10, 2022 at 13:7 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Toneme

© 2022 - 2024 — McMap. All rights reserved.