简体   繁体   中英

What am I doing incorrectly?

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 receive 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM