3

I am trying to toggle visibility of signup and signin boxes if sign in and sign up buttons are clicked. I am trying to use only pure javascript.

I wrote simple html and javascript as below:

<div>
    <button class="signin">sign in</button><button class="signup">sign up</button>

    <div class="signin-box" style="display: none;">
        <form class="signin-form">
            <label>username<input></label><label>password<input></label><button type="submit">signin</button>
        </form>
    </div>

    <div class="signup-box" style="display: none;">
        <form class="signup-form">
            <label>username<input></label><label>password<input></label><button type="submit">signup</button>
        </form>
    </div>
</div>

javascript part:

var signupButton = document.getElementsByClassName('signup')[0];
var signinButton = document.getElementsByClassName('signin')[0];
var signupBox = document.getElementsByClassName('signup-box')[0];
var signipBox = document.getElementsByClassName('signin-box')[0];

console.log("box: ", signupBox, "button: ",signupButton);

var toggleVisible = function(item){
    if (item.style.display === 'none'){
        return item.style.display = 'block';
    }else{
        return item.style.display = 'none';
    }
};

window.onload = function(){
    signupButton.onclick = toggleVisible(signupBox);
    signinButton.onclick = toggleVisible(signipBox);
};

The problem here is that the javascript toggleVisible is automatically activated even if i never clicked the buttons.

as a result, the signin-box and signup-box both gets display:block property.

How do i solve this problem?

2
  • The title is a bit misleading...what does this have to do with MongoDB? Commented Jun 17, 2015 at 21:11
  • @JasonCust oops.. fixed it Commented Jun 17, 2015 at 21:13

2 Answers 2

5

You're calling the function, not passing it in. Just wrap your function call in an anonymous function:

signupButton.onclick = function() {
    toggleVisible(signupBox);
};

If you don't care about older browsers, you can also simplify your code a little if you put your JavaScript at the bottom of the <body> tag and add a rule to your CSS:

document.querySelector('.signup').addEventListener('click', function() {
    document.querySelector('.signup-box').classList.toggle('hidden');
}, false);

document.querySelector('.signin').addEventListener('click', function() {
    document.querySelector('.signin-box').classList.toggle('hidden');
}, false);

And the CSS:

.hidden {
    display: none;
}
Sign up to request clarification or add additional context in comments.

Comments

3

I would recommend to use a standard JavaScript method addEventListener() to attached onclick event listener to the button. It has following advantages over different solution:

You can attach an event handler to an element without overwriting existing event handlers.

You can add many event handlers of the same type to one element, i.e two "click" events.

In your code it will look like

window.onload = function(){
    signupButton.addEventListener("click", function() { toggleVisible(signupBox); });
    signinButton.addEventListener("click", function() { toggleVisible(signipBox); });
};

Current code invokes toggleVisible(..) method and assigns its result to the button attribute, which is not one would expect.

signupButton.onclick = toggleVisible(signupBox);

Comments

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.