SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking Checkbox enabling another field help

    Hello,

    I am new to JS and trying to write a function which would allow a checkbox to enable a text field on a form which is set to disabled. What I am trying to get is to enable to disable the text field using the checkbox. My code works but then I am unable to uncheck the checkbox. My code:

    function alertBox() {

    if(document.form1.checkbox.checked=true) {
    document.form1.textbox.disabled=false;
    }
    else {
    if(document.form1.checkbox.checked=false) {
    document.form1.textbox.disabled=true;
    }
    }
    }


    Any help would highly be appreciated.

    Thanks.

  2. #2
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <script type="text/javascript">
    <!--//--><![CDATA[//><!--
    function able(el1,el2){
    document.getElementById(el1).onclick=function(){
    if(this.checked){
    document.getElementById(el2).removeAttribute('disabled')
    }
    else{document.getElementById(el2).setAttribute('disabled','true')
    }
    }
    }
    function init(){able('a1','a2')}
    window.onload=init
    //--><!]]>
    </script>
    <label for="a1">Check to allow input bellow
    <input type="checkbox" id="a1" /></label><br />
    <label for="a2">Checkbox above has to be checked to edit
    <input type="text" id="a2" disabled="disabled" /></label>

  3. #3
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot Mirek.


Bookmarks

Posting Permissions

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