LIVE CODE: http://jsfiddle.net/vy4nY/
I'm following this challenge, but I'm having some issues. I'm trying to make the Email Address box only appear when the checkbox is clicked on. What have I done incorrectly?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>JavaScript Challenges</title>
<style type="text/css">
#emailpara {visibility:hidden;}
</style>
</head>
<body>
<form action="">
<fieldset>
<legend>Email subscriptions</legend>
<p id="subscribepara">
<label>
<input type="checkbox" name="subscribe" id="subscribe">
Yes! I would like to rece开发者_如何学运维ive the occasional newsletter via email.
</label>
</p>
<p id="emailpara">
<label>
Email Address:
<input type="text" name="email" id="email">
</label>
</p>
</fieldset>
</form>
<script type="text/javascript">
document.getElementById('subscribe').onclick = (document.getElementById('subscribe').checked ? (document.getElementById('emailpara').style.visibility = 'visible') : (document.getElementById('emailpara').style.visibility = 'hidden'));
</script>
</body>
</html>
The onclick handler should be a function. Use:
document.getElementById('subscribe').onclick = function() {
document.getElementById('emailpara').style.visibility = this.checked ? 'visible' : 'hidden';
}
You need to have it as function:
document.getElementById('subscribe').onclick = function() {
document.getElementById('subscribe').checked ? document.getElementById('emailpara').style.visibility = 'visible' : document.getElementById('emailpara').style.visibility = 'hidden';
}
Updated jsFiddle: http://jsfiddle.net/yahavbr/vy4nY/1/
精彩评论