Reputation: 21
I wanted to select an option from a form. If I select this option, Javascript should check if the value/content is ex. "B".
How can Javascript now check if the value/content of this option is B or is not B?
<script type="text/javascript">
var text = document.form1.fahrstunden;
function a(){
if (document.form1.klasse.options[klasse.option.value=B].selected == true) {
alert("fu test");
}
}
</script>
<form onmousemove="a()" id="form1" name="form1"method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="a()" >
<select name="klasse" id="klasse" >
<option value="B" selected="selected">B</option>
</select>
</form>
Upvotes: 2
Views: 124
Reputation: 104795
You can add an onchange
function:
document.getElementById("klasse").onchange = function() {
alert(this.value);
}
You can then compare that value to whatever you need, ex:
if (this.value == "B") {
//value is B, do stuff!
}
Side note: the this.value
is pulled from the value
attribute of the option. If you want the actual text, you can do the following:
var selectedText = this.options[this.selectedIndex].text;
Demo: http://jsfiddle.net/tymeJV/U7KKF/
Upvotes: 2