Forum Moderators: open
I would like the asterisk next to the State field to be hidden if the user chooses a country other than USA or Canada in the country pulldown.
Can this be done without somehow refreshing the browser which obviously won't work?
Thanks. Tim
How about something like this. The onchange event of the selCountry select object fires the setStateReq function, which shows/hides the asterisk which is in the span myspan based upon the currently selected option of selCountry.
I assume that you have some code already to handle required field validation.
<script type="text/javascript">
<!--
//this function shows/hides an asterisk span indicating required zip field
//and accepts a reference to the select object as a parameter
function setStateReq(obj){
//create a locally scoped variable to hold the currently selected option
var curCountry=obj.options[obj.selectedIndex].value
//if country is USA or Canada, show the asterisk span
if (curCountry=='USA' ¦¦ curCountry=='Canada'){
document.getElementById('myspan').style.display='inline'
//otherwise hide the asterisk span
}else{
document.getElementById('myspan').style.display='none'
}
}
//-->
</script>
<style type="text/css">
</style>
</head>
<body>
Country<select name="selCountry" id="selCountry" onchange="setStateReq(this)"><option value="USA">USA</option><option value="Canada">Canada</option><option value="Slobovia">Slobovia</option></select><br />
Zip/Postal Code <input type="text" size="15" id="txtZip" /><span id="myspan">*</span>
</body>
</html>
Hope this helps,
ajkimoto