javascript - onclick: Have to click twice in order for a div to hideshow - Stack Overflow

I have a problem where I have to click twice in order for my JavaScript function to run.HTML for the bu

I have a problem where I have to click twice in order for my JavaScript function to run.

HTML for the button:

<input type="button" id="logintrigger" value="Log In" onClick="hideshow();"/>

JavaScript:

function hideshow() {
    var toggle = document.getElementById('logincontainer');
    toggle.style.display = toggle.style.display == "none" ? "block" : "none";
}

I have been searching around and haven't found anything that helped.

Thank You.

I have a problem where I have to click twice in order for my JavaScript function to run.

HTML for the button:

<input type="button" id="logintrigger" value="Log In" onClick="hideshow();"/>

JavaScript:

function hideshow() {
    var toggle = document.getElementById('logincontainer');
    toggle.style.display = toggle.style.display == "none" ? "block" : "none";
}

I have been searching around and haven't found anything that helped.

Thank You.

Share Improve this question asked Jul 29, 2012 at 19:51 user1512677user1512677 432 silver badges4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Apply style="display:block" explicitly to logincontainer and then change following line : toggle.style.display = toggle.style.display == "block" ? "none" : "block";

The initial state of display property is more likely to be empty. That's why till you explicitly change it - this js function gets "".

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745186023a4615642.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信