Jump to content

[SOLVED] checkbox question


jim.davidson

Recommended Posts

I have checkbox "Other" that if the user checkes it a text field will open. I get that to work, but if the user unchecks it the text box doesn't go away.

 

Can someone tell me what I'm missing? Here's the code. By the way I'm a newbie with javascript so the code might seem messy to you pros

 

<input name="checkbox_other" type="checkbox" class="text_background" id="checkbox_other" value="other"onclick="(this.value=='other') ? document.getElementById('newClientLbl').style.display='' : document.getElementById('newClientLbl').style.display='none'"><label style="display:none" id="newClientLbl">Enter Your Client Type: <input type="text" name="newClient" class="text_background"></label>

 

 

Any help will be appreciated

 

Link to comment
Share on other sites

Putting your javascript inline like that is not recommended and makes your code unwieldly. Just make a function that you can reuse if needed

 

<html>
<head>
  <script type="text/javascript">

    function displayField(fieldID, display)
    {
      document.getElementById(fieldID).style.display = (display) ? 'inline' : 'none';

    }

  </script>
</head>

<body>

<input name="checkbox_other" type="checkbox" class="text_background" id="checkbox_other" value="other" onclick="displayField('newClientLbl', this.checked)">
<label style="display:none" id="newClientLbl">Enter Your Client Type: <input type="text" name="newClient" class="text_background"></label>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.