PDA

View Full Version : Dynamic form


Chon-Ji
06-26-2006, 08:25 AM
I need to make some of my form elements change depending on the action of the user. Here's a sample of my code...


<form>
<select>
<option value="Yes">YES</option>
<option selected value="No">No</option>
</select>

<input type="Text" value="" name="id" disabled />
<input type="Submit" value="Submit" name="submit" disabled />
</form>


The initial selection in the list box is "NO" and the form elements are initially disabled. Whenever the user select "YES" from the list box I need to enable the text field and submit button. Likewise if the user selects "NO" I have to disable the elements. Is there anyway I could do this in javascript?

thanks

pj_anf
06-26-2006, 09:46 AM
Yeah this is a fairly easy and common thing to use javascript for. My prefered method is to hide the items from view entirely unless they select yes. This keeps extra questions they don't need to answer off the screen leaving a cleaner less cluttered look, but keep in mind this is just my opinion and there are plenty of other ways you can use javascript and achieve this result your trying to get.

My code my not be entirely correct, I didn't test it to double check it, so use it as a refrence at worst.


<form>
<select>
<option value="Yes" onclick="document.form[0].id.style='visibility:visible'">YES</option>
<option selected value="No" onclick="document.form[0].id.style='visibility:hidden'">No</option>
</select>

<input type="Text" value="" name="id" style="visibility:hidden"/>
<input type="Submit" value="Submit" name="submit" style="visibility:hidden" />
</form>