views:

120

answers:

4

What is the best way to gray out text inputs on an HTML form? I need the inputs to be grayed out when a user checks a check box. Do I have to use JavaScript for this (not very familiar with JavaScript) or can I use PHP (which I am more familiar with)?

EDIT:

After some reading I have got a little bit of code, but it is giving me problems. For some reason I cannot get my script to work based on the state of the form input (enabled or disabled) or the state of my checkbox (checked or unchecked), but my script works fine when I base it on the values of the form inputs. I have written my code exactly like several examples online (mainly this one) but to no avail. None of the stuff that is commented out will work. What am I doing wrong here?

<label>Mailing address same as residental address</label>
<input name="checkbox" onclick="disable_enable()" type="checkbox" style="width:15px"/><br/><br/>

<script type="text/javascript">

    function disable_enable(){

        if (document.form.mail_street_address.value==1)
            document.form.mail_street_address.value=0;
            //document.form.mail_street_address.disabled=true;
            //document.form.mail_city.disabled=true;
            //document.form.mail_state.disabled=true;
            //document.form.mail_zip.disabled=true;

        else
            document.form.mail_street_address.value=1;
            //document.form.mail_street.disabled=false;
            //document.form.mail_city.disabled=false;
            //document.form.mail_state.disabled=false;
            //document.form.mail_zip.disabled=false;

    }

</script>

EDIT:

Here is some updated code based upon what @Chief17 suggested. Best I can tell none of this is working. I am using value as a test because it works for some reason

            <label>Mailing address same as residental address</label>
        <input name="checkbox" onclick="disable_enable()" type="checkbox" style="width:15px"/><br/><br/>

        <script type="text/javascript">

            function disable_enable(){

                if (document.getElementById("mail_street_address").getAttribute("disabled")=="disabled")
                    document.form.mail_street_address.value=0;
                    //document.getElementById("mail_street_address").removeAttribute("disabled");
                    //document.getElementById("mail_city").removeAttribute("disabled");
                    //document.getElementById("mail_state").removeAttribute("disabled");
                    //document.getElementById("mail_zip").removeAttribute("disabled");

                else
                    document.form.mail_street_address.value=1;
                    //document.getElementById("mail_street_address").setAttribute("disabled","disabled");
                    //document.getElementById("mail_city").setAttribute("disabled","disabled");
                    //document.getElementById("mail_state").setAttribute("disabled","disabled");
                    //document.getElementById("mail_zip").setAttribute("disabled","disabled");
            }

        </script>
+5  A: 

Unfortunately, since you're doing it in response to user input without a form being sent back to the server, you have to do it through JavaScript.

input elements in JavaScript have both readonly and disabled attributes. If you want them completely disabled, you need to use JavaScript (or a library like jQuery) to change the disabled attribute's value to "disabled".

Note that the disabled inputs will not have their values sent to the server when the form is submitted.

R. Bemrose
How is "doing it in response to user input" *unfortunate*?
Dolph
I think he was saying that it is unfortunate for me because I don't know JavaScript and I indicated I would rather use PHP if possible.
typoknig
In that case, you're fortunate to have an excuse to learn some JS!
Dolph
A: 

The easiest way is to use JavaScript to enable or disable the form elements when the checkbox's checked status is changed.

That said, you will still need to filter for that checkbox when handling the post on the server, as some browsers will have JS turned off and thus the change will not happen

Stephen Wrighton
+1  A: 

Deleted my other post entirely and replaced with all the code you should need:

    <script type="text/javascript">

    function disable_enable()
    {
        if(document.getElementById("checkbox").checked != 1)
        {
            document.getElementById("input1").removeAttribute("disabled");
            document.getElementById("input2").removeAttribute("disabled");
            document.getElementById("input3").removeAttribute("disabled");
            document.getElementById("input4").removeAttribute("disabled");
        }
        else
        {
            document.getElementById("input1").setAttribute("disabled","disabled");
            document.getElementById("input2").setAttribute("disabled","disabled");
            document.getElementById("input3").setAttribute("disabled","disabled");
            document.getElementById("input4").setAttribute("disabled","disabled");
        }
    }

    </script>

and

    <label>Mailing address same as residental address</label>
    <input id="checkbox" onClick="disable_enable()" type="checkbox" style="width:15px"/><br/><br/>
    <input type="text" id="input1" />
    <input type="text" id="input2" />
    <input type="text" id="input3" />
    <input type="text" id="input4" />
Chief17
Thank, but I still cannot get that to work either. Any other thoughts?
typoknig
what part exactly isn't working
Chief17
From what I can see none of what you suggested is working. The if statement does not work, nor does the remove/set attribute method. I have edited my question with the updated code.
typoknig
@typoknig – You shouldn't use `[get|set]Attribute` for this, just use `element.disabled`, a boolean. Set it using `element.disabled = [true|false]`, test it using `if (element.disabled)`, like in Kris' answer.
Marcel Korpel
Thanks guys, this one worked. @Marcel, I was wondering why I shouldn't use get/set for something like this? It seems to be working very well, is there a specific reason why I don't want to do it like this? Just a note too, I think the reason some of the other answers were not working for me was because a few of my `id` attributes were messed up in my `input` tags. It always seems to be the part of my code I don't share that is messing me up :)
typoknig
@typoknig – In *this* specific case, it should work, but read bobince's comments on [this answer](http://stackoverflow.com/questions/2775328/alt-attribute-encoding-with-javascript/2775436#2775436) and you know why it's generally better to use the standardized properties like `element.title`, etc. Moreover, the boolean property is easier to set, unset and read than the string used/returned by `[get|set]Attribute`; e.g., what happens when some browser (or yourself) input the value `"true"` (string) to the `disabled` attribute?
Marcel Korpel
+2  A: 

Basically, loop through inputs, check if they're checkboxes, add event handlers...

Working sample in plain old javascript: http://www.theredhead.nl/~kris/stackoverflow/enable-or-disable-input-based-on-checkbox.html

the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
    <head>
        <title>Enable/disable input based on checkbox</title>
        <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">
                Textbox 1: 
                <input id="textbox_1" type="text" value="some value" />
            </label>
            <br />
            <input id=="checkbox_1" type="checkbox" rel="textbox_1" />
            <label for="checkbox_1">Enable textbox 1?</label>
            <hr />
        <form>
    </body>
</html>
Kris
note that i'm using the rel attribute on the checkbox to indicate what textbox i want to manipulate.
Kris
Also note that `addEventListener` doesn't work in [my favorite browser](http://ihateinternetexplorer.com/). Moreover, a decrementing `while` loop is [faster](http://blogs.sun.com/greimer/entry/best_way_to_code_a) than this `for` loop.
Marcel Korpel
I tried this but I am not using the same doc type, so I am getting an error using the `rel` attribute :(
typoknig
@Kris, I wanted to add that the bit of code you posted has give me a lot to think about, as I am a JavaScript noobie. @Chief17 latest answer was much less complex though, and it got me going. Thanks for your help!
typoknig
@Marcel, i _always_ mixup addEventHandler and addEventListener, only tested in safari. I was going for readable as opposed to bloody fast here, decrementing while loop isn't half as readable. I concur on the browser front though ;)
Kris