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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    enable textbox by checkbox #compact code

    I'm not that familiar with javascript, so I'm probably overlooking a silly mistake.
    I've tried this, but it doesn't work:
    Code:
    ...
    <input type="checkbox" name="chk_optie[]" value="56" title="" onclick="f_txt_disable('prijs_56')">Alarmsysteem
    <input type="textbox" name="prijs_56" length=8 disabled><br />
    ...
    <script language="javascript">
    	function f_txt_disable (txt_naam) {
    		document.getElementByName(txt_naam).disabled = !document.getElementByName(txt_naam).disabled;
    	}
    </script>
    ...
    What am I doing wrong?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,312
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Life is so much easier if you use IDs for element of interest. You can do this inline also:
    Code:
    <input type="checkbox" name="chk_optie[]" value="56" title="" onclick="f_txt_disable('prijs_56')">Alarmsysteem
    <input type="textbox" name="prijs_56" id="prijs_56" length=8 disabled><br />
    
    <script>
    	function f_txt_disable (txt_naam) {
    		document.getElementById(txt_naam).removeAttribute("disabled");
    	}
    </script>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey thanks, that works! :-)

    The only thing is that I also want the textbox to disable itself automatically if the checkbox is unchecked again, so I adjusted the code as follows:
    Code:
    <script language="javascript">
    	function f_txt_disable (txt_id) {
    		document.getElementById(txt_id).disabled = !document.getElementById(txt_id).hasAttribute("disabled");
    	}
    </script>

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You can do it inline:
    Code:
    <input type="checkbox" name="chk_optie[]" value="56" title="" onclick="this.form.prijs_56.disabled=!this.form.prijs_56.disabled;" />
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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