0

I have this working currently, when you press the check box, the drop down (textbox_1) becomes disabled. My question is, how to retrofit this code to also disable textbox_2 by the same click...

<html>
    <head>
        <script type="text/javascript">
        // setup a bit of code to run after the document has loaded. (note that its set on window)
        window.addEventListener('load', function(){
            potential_checkboxes = document.getElementsByTagName('input');
            for(i = 0; i < potential_checkboxes.length; i ++) {
                element = potential_checkboxes[i];
                // see if we have a checkbox

                if (element.getAttribute('type') == 'checkbox') {
                    // initial setup
                    textbox = document.getElementById(element.getAttribute('rel'));
                    textbox.disabled = element.checked;

                    // add event handler to checkbox
                    element.addEventListener('change', function() {
                        // inside here, this refers to the checkbox that just got changed
                        textbox = document.getElementById(this.getAttribute('rel'));
                        // set disabled property of textbox to not checked property of this checkbox
                        textbox.disabled = this.checked;
                    }, false);
                }               
            }
        }, false);
        </script>
    </head>
    <body>
        <h1>Enable/disable input based on checkbox.</h1>

        <form>
            <label for="textbox_1">
            <SELECT class="enteredYear" id="textbox_1" name=year>
            <OPTION selected value="">Year</OPTION>
            <OPTION value=2013>2014</OPTION>
            <OPTION value=2013>2013</OPTION>
            <OPTION value=2012>2012</OPTION>
            </SELECT>
            </label>
            <label for="textbox_2">
                Textbox 1: 
                <input id="textbox_2" type="text" value="some value" />
            </label>

            <br />
            <input id=="checkbox_1" type="checkbox" rel="textbox_1"/>
            <label for="checkbox_1">I have a classic car.</label>
            <hr />
        <form>
    </body>
</html>
5
  • 3
    Reason more why you should start using jQuery! Commented Jul 30, 2013 at 20:41
  • I agree, i'm learning as I go, this is just something I need to figure out soon! Commented Jul 30, 2013 at 20:43
  • The problem you face is that this relies of rel property of the checkbox input. So the question is do you care about the ability to specify the target id to be disabled by specifying the rel value? Commented Jul 30, 2013 at 20:44
  • Im a PHP/MYSQL person, but loving the power of javascript - Ive used jQuery for its ajax function, since php is my thing! Commented Jul 30, 2013 at 20:44
  • Mike - I dont "care" but this is the code I found and changed a bit to work for me. I just need it to disable both inputs, and from that I can see how it worked! Commented Jul 30, 2013 at 20:45

4 Answers 4

3

Just "grab" the textbox_2 using document.getElementById('textbox_2'), and disable it inside your change event listener for the checkbox.

// add event handler to checkbox
element.addEventListener('change', function() {
    // inside here, this refers to the checkbox that just got changed
    textbox = document.getElementById('textbox_1');

    // ---------------------------------------------
    // Grabs the textbox_2 element
    textbox2 = document.getElementById('textbox_2');
    // ---------------------------------------------

    // set disabled property of textbox to not checked property of this checkbox
    textbox.disabled = this.checked;

    // ------------------
    // Disables textbox_2
    // ------------------
    textbox2.disabled = this.checked;
}, false);    
Sign up to request clarification or add additional context in comments.

4 Comments

A good answer, but I would suggest that if you are going to hard-code the textbox_2 id, that you would go ahead and remove the reliance on the checkbox's rel property for disabling textbox_1 just such the behavior is consistent.
@MikeBrant You're right, I missed that. I tend to only change what the user wants to be changed without touching what already worked for him though, but in this case it's justifiable for the sake of consistency.
Any chance you would show me how to change the click to a drop down, and if the dropdown value is yes, it has the same function as the click?
@user1789437 - just use a handler for the onChange event of the select element. In the handler, check to see if the value is the one that corresponds to your desired option. If so, call the code that disables the inputs. I'd suggest that you name the functions, rather than using anonymous ones. This will make calling the same code from two places a snap.
1

Add a CSS class name to the elements you want to disable and use:

var checks = document.getElementsByClassName('disableme')

"checks" will then by an array containing those elements. Loop, disable.

Comments

0
element.addEventListener('change', function() {
  // inside here, this refers to the checkbox that just got changed
  textbox = document.getElementById(this.getAttribute('rel'));
  textbox.disabled = this.checked;
  textbox2 = document.getElementById('textbox_2');
  textbox2.disabled = this.checked;
}, false);

Comments

0

If you want to stick with a similar approach, you could do something like this:

<input id=="checkbox_1" type="checkbox" rel="textbox_1,textbox_2"/>

element.addEventListener('change', function() {
    var ids = this.getAttribute('rel').split(",");
    for(var i = 0; i < ids.length; i++) {
        textbox = document.getElementById(ids[i]);
        // set disabled property of textbox to not checked property of this checkbox
        textbox.disabled = this.checked;
    }
}, false);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.