Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts

    Need help with checkbox yes/no

    Looking for a simple method to Toggle textbox yes/no using CheckBox

    This example does not work!
    But I hope this will help explain what I am trying to do

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    function YesOrNo() {
      if(document.formname.checkboxname.checked)
      {
      document.formname.textname.disabled='Yes';
      }
      else
      {
      document.formname.textname.disabled='No';
    }}
    </SCRIPT>
    
    <form name="formname">
       <input type="text" size="10" name="textname">
       <input type="checkbox" onclick="YesOrNo()" name="checkboxname" value="Yes">
    </form>

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Try this
    Code:
    var YesOrNo = (function() {
      if(document.formname.checkboxname.checked) {
        document.formname.textname.disabled = 'disabled';
      } else {
        document.formname.textname.disabled = '';
      }
    });
    And for the form use beter markup
    Code:
    <form method="post" action="" name="formname">
       <input type="text" size="10" name="textname" value="" />
       <input type="checkbox" onclick="YesOrNo();" name="checkboxname" value="Yes" />
    </form>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    <SCRIPT LANGUAGE="JavaScript">
    PS: The language attribute is deprecated. Use type="text/javascript" instead.
    Last edited by abduraooft; 12-12-2010 at 04:07 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •