user1403763
user1403763

Reputation: 21

Javascript disabling input field

Haven't been around in a while. Got a hot project I'm working on and I can't seem to figure out how to disable an input text field. The situation is that I have a form that is filled out and then when it is submitted I leave the form where it is but disable the input fields so it can't be changed. So the user can continue to see what they have submitted.

<html> 
    <head> 
    <script> 
    function enableDisable() {
        var disable = true; 
        var arglen = arguments.length; 
        var startIndex = 0; 
        var frm = document.example1; //change appropriate form name

        if (arglen > 0){ 
            if (typeof arguments[0] == "boolean") { 
                disable = arguments[0]; 
                if (arglen > 1) {
                    startIndex = 1; 
                }
            } 
            for (var i = startIndex; i < arglen; i++) {
                obj = eval("frm." + arguments[i]); 
                if (typeof obj=="object") {
                    if (document.layers) {
                        if (disable) { 
                            obj.onfocus = new Function("this.blur()"); 
                            if (obj.type == "text") {
                                obj.onchange = new Function("this.value=this.defaultValue"); 
                            }
                        }
                        else {
                            obj.onfocus = new Function("return");
                            if (obj.type == "text") {
                                obj.onchange = new Function("return");
                            }
                        }
                    }
                    else {
                        obj.disabled=disable;
                    }
                }
            }
        }
    } 
    </script> 
    </head> 
    <body> 
        <form name="example1"> 
            Text Field: <input type="text" name="text1"> 
            <br> 
            <input type="submit" name="control1" onclick="enableDisable(this.submit, 'text1', 'submit', 'select1')"> 
        </form> 
    </body> 
</html> 

Upvotes: 2

Views: 4067

Answers (1)

Er. Varun Dublish
Er. Varun Dublish

Reputation: 41

I think you want the text field to be a read only field.

there is a difference between a disabled text field and a read only text field.

READONLY and DISABLED both remove the functionality of the input field, but to different degrees. READONLY locks the field: the user cannot change the value. DISABLED does the same thing but takes it further: the user cannot use the field in any way, not to highlight the text for copying, not to select the checkbox, not to submit the form. In fact, a disabled field is not even sent if the form is submitted.

So you should look into this post for more info regarding the same. http://www.htmlcodetutorial.com/forms/_INPUT_DISABLED.html

Upvotes: 3

Related Questions