Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Form reset does not restore disabled properties

When some elements in a HTML form are initially disabled, but get enabled and their value changed afterwards, if user clicks the form's reset button, the values of those elements get restored to their initial values, but they're still enabled although they were disabled originally. Why is it so, and how can I ensure that form reset resets those elements to be disabled?

JSfiddle here: http://jsfiddle.net/d872N/

Code example here:

<html>
<head>
<script>
function enableDisable()
{
    var combo1 = document.getElementById("a");
    var combo2 = document.getElementById("b");
    if (combo1.value == 1)
        combo2.disabled = true;
    else
        combo2.disabled = false;
}
</script>
</head>
<body>
<form name="form" action="dummy">
    <select id="a" onchange="enableDisable();">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <select disabled id="b">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <input type="submit" value="submit" />
    <input type="reset" value="reset" />
</form>
</body>
</html>
like image 248
h9lpq0u Avatar asked Oct 21 '22 16:10

h9lpq0u


1 Answers

The reset() method only restores a form elements default values and will not change the disabled state.

Read some documentation here https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement.reset

I would suggest that you need to write your own reset method which calls HTMLFormElement.reset() and then resets the disabled state.

e.g.

New JavaScript code:

var hardReset = function(){    
    document.forms["form"].reset();
    document.getElementById("b").setAttribute("disabled","disabled");    
};

New HTML:

<input type="button" value="reset" onclick="hardReset();" />

Please see an update of your jsFiddle

like image 112
Tim B James Avatar answered Nov 01 '22 17:11

Tim B James