175
rated 0 times
[
179]
[
4]
/ answers: 1 / hits: 23698
/ 11 Years ago, thu, january 30, 2014, 12:00:00
I am trying to make a select field mandatory on a web page. I know how to do it with help of JS and form attribute 'onsubmit' and returning the function. But the problem is that form code is already written and I dont know how to add attribute now. Let me know if I can append attribute dynamically from JS.
The other way I tried is to call the JS after page loaded. But this isnt making the field mandatory and form can be submitted.
Following is my code..
<!DOCTYPE html>
<html>
<head>
<script>
function f1()
{
var countryValue = document.getElementById('count ID').value;
if (countryValue == )
{
alert(field value missing);
return false;
}
var stateValue = document.getElementById('state ID').value;
if (stateValue == )
{
alert(state field value missing);
return false;
}
}
</script>
</head>
<body>
<form method = post action = 33.html>
Country: <input type=text id=count ID>
state: <select id=state ID>
<option></option>
<option value=ap>ap</option>
<option value=bp>bp</option>
</select>
<br>
<input type = submit>
</form>
<script>window.onload=f1</script>
</body>
</html>
Please help.
More From » html