Jump to content

Form Input Enable/Disable


hybrid kill3r

Recommended Posts

I'm trying to make a form, one field has two radio input options, another is a text input. The first field asks a question and has the options of Yes or No. The text input that comes after it should only be enabled if the user selects the Yes option. How do I do this with this bit of code?

<script type="text/javascript">function disable_enable(){if (document.all || document.getElementById){if (document.register.clanpass.disabled==true)document.register.clanpass.disabled=falseelsedocument.register.clanpass.disabled=true}}</script>

Link to comment
Share on other sites

I'm trying to make a form, one field has two radio input options, another is a text input. The first field asks a question and has the options of Yes or No. The text input that comes after it should only be enabled if the user selects the Yes option. How do I do this with this bit of code?
<script type="text/javascript">function disable_enable(){if (document.all || document.getElementById){if (document.register.clanpass.disabled==true)document.register.clanpass.disabled=falseelsedocument.register.clanpass.disabled=true}}</script>

I would use document.getElementById to reference the element:
if (document.getElementById('clanpass').disabled==true)document.getElementById('clanpass').disabled=falseelsedocument.getElementById('clanpass').disabled=true

Also, I would simplify the code by making use of the fact that the value of the disabled attribute value is already boolean true/false. This means you can have a single statement instead of an if/else:

document.getElementById('clanpass').disabled=(!document.getElementById('clanpass').disabled)

Link to comment
Share on other sites

Sorry, realised after posting, your question was to do with the radiobuttons, which your function doesn't refer to yet! :) There are various ways to check which radiobutton is checked, but assuming you have radiobuttons such as:

<input type="radio" name="r" value="yes" checked="checked" onclick="disable_enable(this)">Yes<input type="radio" name="r" value="no" onclick="disable_enable(this)">No

then the javascript could become:

function disable_enable(radio){   document.getElementById('clanpass').disabled=!(radio.value=='yes');}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...