2

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>
1
  • Please re-format your question, the line-breaks are making me crazy :) Commented Jun 11, 2013 at 18:12

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Glad i could help, be sure to mark it as correct when you can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.