admin管理员组文章数量:1387299
I would like the button on my web page to toggle the visibility of my two check-boxes.
How do I go about implementing such functionality?
<html>
<head>
<script language=javascript>
function validate(chk){
if (chk.style.visibility == 'visible')
chk.style.visibility = 'hidden';
else
chk.style.visibility = 'visible';
}
</script>
</head>
<body>
<form><span id=t style.visibility="visible" >
<input type=checkbox name=chk1>Please Check Me</span>
<input type=checkbox name=chk1>Please Check Me</span>
<p><input type=button value="check" onclick="return validate(t);">
</form>
</body>
</html>
I would like the button on my web page to toggle the visibility of my two check-boxes.
How do I go about implementing such functionality?
<html>
<head>
<script language=javascript>
function validate(chk){
if (chk.style.visibility == 'visible')
chk.style.visibility = 'hidden';
else
chk.style.visibility = 'visible';
}
</script>
</head>
<body>
<form><span id=t style.visibility="visible" >
<input type=checkbox name=chk1>Please Check Me</span>
<input type=checkbox name=chk1>Please Check Me</span>
<p><input type=button value="check" onclick="return validate(t);">
</form>
</body>
</html>
Share
Improve this question
edited Feb 25, 2010 at 17:53
munity wiki
8 revs, 6 users 46%
nisnis84 0
2 Answers
Reset to default 1It's a good practice to keep your JavaScript code and CSS out of your HTML.
<span id="t">
<input type="text" />
<input type="text" />
</span>
<button id="toggler">Toggle</button>
For the JavaScript code, we look up the two elements via their ID. We then attach some logic to the onclick
method of the button itself. This logic checks the present value of the display
style on the span element. If it's visible, we hide it. If it's hidden, we show it.
document.getElementById("toggler").onclick = function(){
var s = document.getElementById("t");
(s.style.display == "none") ? s.style.display = "" : s.style.display = "none";
};
Online demo: http://jsbin./iyiki/2/edit
You're not giving any object to the function. A quick fix is:
return validate(document.getElementById('t'));
Also, this HTML is wrong. It should be:
<span id="t" style="visibility:visible" >
本文标签: javascriptChanging checkbox visibilityStack Overflow
版权声明:本文标题:javascript - Changing checkbox visibility - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744484778a2608386.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论