I have a php form which has a dropdown list and a text which show and hide based on drop down list value. The problem is when I click submit button and there is validation error the onload function work and the text hide ,,, it should stay visible or invisible depending on the drop down list.
Here is what Im doing:
<script type="text/javascript">
function showfield(name){
if(name != 'High School')
document.getElementById('div1').style.display="block";
else
document.getElementById('div1').style.display="none";
}
function hidefield() {
document.getElementById('div1').style.display='none';
}
<script>
Im calling the hidefield function here on body on load:
<body onload = "hidefield()">
and the other function on the drop downlist
<select name="education" size="1" class="text" id="education" onchange="showfield(this.options[this.selectedIndex].value)">
Hope some one can help.
just modify your hidefield function to only hide it if it needs to
function hidefield() {
var select = document.getElementById("education");
if (select.options[select.selectedIndex] != 'High School')
document.getElementById('div1').style.display='none';
}
Script is missing the closing tag,
</script> after the function hidefield()