Enable and disable HTML form elements with Javascript by ID
Here's a simple hint on how to dynamically activate/deactivate (or enable/disable, however you want to call it) HTML/XHTML form elements (input boxes, select lists, radio buttons, checkboxes).
This way you can, for example enable a certain form element after a specific Javascript/JS action or disable it if you do not want the user to be able to modify values in the respective part of your form.
It works by simply enabling/disabling the element through its HTML ID, which you'll have to specify.
For specifically disabling the element use:
document.getElementById('element_id').disabled = true;
And for enabling it use:
document.getElementById('element_id').disabled = false;
Keep in mind though that disabled Form Elements can give your form submission headaches as you might have troubles with the fact that in some cases disabled values don't get submitted!
There are 2 decent workarounds I can think of at the moment:
The first one simply would be re-enabling the elements right before the form submission - this might not work in every case though.
The second solution, which I am mostly using for the cases where I need to deactivate form fields: Simply hide the form fields instead of disabling them and show the value/text of the field/element as simple text.
That's it, enjoy.





Stefan: I am a complete
Stefan:
I am a complete newbie to javascripting. I am looking for help in writing code for a form. I have two radio buttons and I want to select one and gray out the other. I understand the code above, but don't understand how to put it into a working script. Can you help?
function Disab(val) {
frm=document.forms[0]
if(val=="t_bone")
{frm.ny_strip.disabled=true;frm.t_bone.disabled=false}
else {frm.t_bone.disabled=false;ny_strip.disabled=true}
}
T_Bone
NY_Strip
thanks,
K. Crowley
Post new comment