Dave's Brain

Browse - Programming Tips - What HTML style do I change to disable a form input control?

Date: 2008jun12
Platform: web

Q.  What HTML style do I change to disable a form input control?

A.  You can control a great many things with styles but whether
something is enabled or disabled isn't one.  There is a way but
not with styles.

Regular HTML:

<input type=text name=stuff disabled>
Simply, adding "disabled" makes an input disabled. But usually you want to do it with javaScript based on changing situation. In that case do this:
<script> // Helper function getByName(name) { var a = document.getElementsByName(name) if (a.length < 1) return null; return a[0]; } function enableOrDisableIt() { var obj = getByName('stuff'); // I like searching by name since names are going to be unique in a <form> if (something) { obj.disabled = true; // Do NOT do obj.style.disabled = true; -- WRONG } else { obj.disabled = false; } } </script>

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.