Forum Moderators: open
I have a hide/show script on a form controlled by a radio button that lets say finds out if someone has a drivers licence
If the answer is
1) yes then it asks them for the class of their license as well as their license number
2) No then it doesn't ask them anything
And that all works beautifully to a point, I have server side error checking set up which works fine, the only problem is that if the form is rejected by the server because a required value hasn't been filled in (anywhere in the form) the Div will reset to its original status, as in the radio button will be ticked for yes but the associated questions in the div are hidden, i can't seem to find a way to preserve the state of the div
This is the code used in my head section
<script type="text/javascript">
<!--function changeDiv(the_div,the_change)
{
var the_style = getStyleObject(the_div);
if (the_style!= false)
{
the_style.display = the_change;
}
}
function hideAll()
{
changeDiv("married_questions","none");
changeDiv("single_questions","none");
}
function hideAll2()
{
changeDiv("passport","none");
changeDiv("n_passport","none");
}
function getStyleObject(objectId) {
if (document.getElementById && document.getElementById(objectId)) {
return document.getElementById(objectId).style;
} else if (document.all && document.all(objectId)) {
return document.all(objectId).style;
} else {
return false;
}
}
// -->
</script>
and here is the code that controls the actual questions,
Do you have a current drivers licence<br>
<input type="radio" name="v_lic" <?php if($v_lic == 'yes') echo "checked ";?>value="yes" onClick="hideAll3(); changeDiv('licence','block');">yes
<input type="radio" name="v_lic" <?php if($v_lic == 'no') echo "checked ";?>value="no" onClick="hideAll3();">no
<div id="licence" style="margin-left:30px;display:none">
<br>Please enter your licence number here<br>
<INPUT TYPE="TEXT" NAME="v_licn" VALUE="<?= $v_licn?>" SIZE="30" MAXLENGTH="30" ><br>
Please enter your licence class<br>
<INPUT TYPE="TEXT" NAME="v_licc" VALUE="<?= $v_licc?>" SIZE="30" MAXLENGTH="30" >
</div>
A very inelegant solution but I added this within the javascript section of my head section
<?php
if($v_lic == 'yes') echo "
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload!= 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
hideAll3(); changeDiv('licence','block');
})
";
?>
Cheers