1

I have two buttons, one passes true argument, another does not. Both buttons disappear when I press on them. But what exactly is going on behind the scenes for each button?

If this code looks familiar to you, it's from JavaScript: The Definitive Guide 6th Edition. Great book!

One more thing. In C++ and Java omitting an argument would result in a compile-error. In C# you could change the second argument to be optional and it would work. In JavaScript I didn't have to do that. Are all arguments in JavaScript optional?

JavaScript:

function hide(e, reflow) {
    if (reflow) {
        e.style.display = "none";
    }
    else {
        e.style.visibility = "hidden";
    }
}

HTML:

<button onclick="hide(this,true); debug('hide button 1');">Hide1</button>
<button onclick="hide(this); debug('hide button 2');">Hide2</button>
2
  • I don't get your question. Behind the scenes: when you click button 1 - it disapears because of e.style.display = "none"; and when you click Button 2 - it disapears because of e.style.visibility = "hidden"; Commented May 10, 2011 at 17:30
  • @Dmitriy I am new to JavaScript and CSS. My question was what is the difference between the two statements. lord_t made it clear as to what happens when you do display = "none" and visibility = "hidden". Commented May 10, 2011 at 17:35

2 Answers 2

2

Yes, all arguments in JavaScript are optional.

With display:none is something similar to width:0; height:0, and if you set the visibility to hidden button stay on it's place, but it is invisible.

Sign up to request clarification or add additional context in comments.

1 Comment

I've made my 2nd button 500px wide to see the difference and it's clear to me now. Thanks!
1

Yeap!

In Javascript you have all arguments optional. Actually you don´t need to name your arguments. There is a implicit var called "arguments" where you can find an array of your arguments. You can ever access your arguments through this var.

Take a look at: https://developer.mozilla.org/en/JavaScript/Reference/functions_and_function_scope/arguments

1 Comment

And about the main question: - visibility: you can hide your element and preserve the space where it was, leaving a empty gap in the layout; - display: you can control how your control will appears on the page, as a block, as an inline element, or even so, hiding it;

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.