![]() |
Hide or Display Form Field when page loaded
I have a form with three fields. When the user changes the value of field1, the form displays either field2 or field3. This works fine for NEW records.
However, I'm running into a problem when trying to EDIT an existing record. When the form is loaded, I need to test the value of field1 and display either field2 or field3. I've tried using the form onload event, but do not know how to access the value of field1. I'm using Javascript and div tags. This is what I have so far. <script type="text/javascript"> function hide(obj) obj1 = document.getElementById(obj); obj1.style.display = 'none'; } function show(obj) { obj1 = document.getElementById(obj); obj1.style.display = 'block'; } function show_other(optionValue) { if(optionValue=='C') { show('divClass');} else{hide('divClass'); } if(optionValue=='W') { show('divWorkshop');} else{hide('divWorkshop'); } } </script> /* Field1 - is a drop down list */ <select name="ClassType" size="1" onchange="show_other(this.value)"> ... </select> /* Field2 */ <div id="divClass"> ... </div> /* Field 3 */ <div id="divWorkshop"> ... </div> Appreciate any assistance. Thanks Peter |
Hello ,
Is that value is stored in the table ?? if so please use retrieve the value to the variable and then check it. use ajax to retrieve value from database . |
Code:
<script type = "text/javascript">Quizmaster: In which English county is the Cornish language spoken? Contestant: Devon. |
Yes, the value of field1 is stored in a database table.
I've never used AJAX, so I was hoping there would be a simple solution with Javascript and div tags. Regards Peter |
Thank you for the code Philip.
Your solution works fine in IE, but Firefox does not like this line - var val = document.getElementById("ClassType").value; It aborts with this error message: Error ``document.getElementById("ClassType") is null What do you think? Regards Peter |
I think you forgot to add id = "ClassType" to the element. Philip_M marked this part BLUE
|
Yes, you are absolutely right.
The script is now working perfectly in IE and Firefox. Thank you for your help. Here is the final working version. <script type="text/javascript"> function checkField() { var val = document.getElementById('ClassType').value; if(val=='C') { show('divClass'); hide('divWorkshop'); } else { show('divWorkshop'); hide('divClass'); } } function hide(obj) { var obj1 = document.getElementById(obj); obj1.style.display = 'none'; } function show(obj) { var obj1 = document.getElementById(obj); obj1.style.display = 'block'; } </script> <body onload=checkField();> /* Field1 - is a drop down list */ <select name="ClassType" id = "ClassType" size="1" onchange="checkField(this.value)"> ... </select> /* Field2 */ <div id="divClass"> ... </div> /* Field 3 */ <div id="divWorkshop"> ... </div> |
BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.
|
| All times are GMT +1. The time now is 08:06 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.