43

In my latest program, there is a button that displays some input popup boxes when clicked. After these boxes go away, how do I hide the button?

1
  • 1
    how are you making the boxes go away? If you are making them go away wih javascript, it is usually very simple to make something else dissapear. Commented Dec 30, 2011 at 23:44

12 Answers 12

73

You can set its visibility property to hidden.

Here is a little demonstration, where one button is used to toggle the other one:

<input type="button" id="toggler" value="Toggler" onClick="action();" />
<input type="button" id="togglee" value="Togglee" />

<script>
    var hidden = false;
    function action() {
        hidden = !hidden;
        if(hidden) {
            document.getElementById('togglee').style.visibility = 'hidden';
        } else {
            document.getElementById('togglee').style.visibility = 'visible';
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

element.classList.add("hide-me"); can anyone explain this can hide the element ?
24
visibility="hidden"

is very useful, but it will still take up space on the page. You can also use

display="none"

because that will not only hide the object, but make it so that it doesn't take up space until it is displayed. (Also keep in mind that display's opposite is "block," not "visible")

Comments

7

Something like this should remove it

document.getElementById('x').style.visibility='hidden';

If you are going to do alot of this dom manipulation might be worth looking at jquery

1 Comment

This doesn't remove the dom but hides it.
4
document.getElementById('btnID').style.visibility='hidden';

Comments

2
//Your code to make the box goes here... call it box
box.id="foo";
//Your code to remove the box goes here
document.getElementById("foo").style.display="none";

of course if you are doing a lot of stuff like this, use jQuery

Comments

2

If the space on that page is not disabled then put your button inside a div.

<div id="a1">
<button>Click here</button>
</div>

Using Jquery:

<script language="javascript">
$("#a1").hide();
</script>

Using JS:

<script language="javascript">
document.getElementById("a1").style.visibility = "hidden";
document.getElementById("a1").style.display = "none";
</script>

Comments

1

when you press the button so it should call function that will alert message. so after alert put style visible property . you can achieve it using

function OpenAlert(){
        alert("Getting the message");
        document.getElementById("getMessage").style.visibility="hidden";
        
    }
 <input type="button" id="getMessage" name="GetMessage" value="GetMessage" onclick="OpenAlert()"/>

Hope this will help . Happy to help

Comments

1

function popAlert(){
        alert("Button will be hidden on click");
        document.getElementById("getMessage").style.visibility="hidden";
        
    }
   h1 {
            color: #0000ff;
        }
<h1>KIAAT</h1>
    <b>Hiding a button in Javascript after click</b>
    <br><br>
<input type="button" id="getMessage"  value="Hide Button OnClick" onclick="popAlert()"/>

Comments

1

You can use this code:

btnID.hidden = true;

Documentation:
JavaScript
HTML

Comments

0

If you are not using jQuery I would suggest using it. If you do, you would want to do something like:

$( 'button' ).on(
   'click'
   function (  )
   {
       $( this ).hide(  );
   }
);

Comments

0
<script>
    $('#btn_hide').click( function () {
      $('#btn_hide').hide();
    });
</script>
<input type="button" id="btn_hide"/>

this will be enough

1 Comment

Line 3 is missing a close quote
0
var start = new Date().getTime();
while ((new Date().getTime() - start) < 1000){
  } //for 1 sec delay

1 Comment

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.