user3389990
user3389990

Reputation: 13

Displaying option selected from dropdownlist

Trying something that sounds simple but not working:

Allow a user to select an option from a dropdownlist and then have this displayed as an alert each time the user changes it. Here's what I've got so far:

<select name="pickSort" id="chooseSort" onchange="changedOption">
    <option value="lowHigh" id="lowHigh">Price Low-High</option>
    <option value="highLow" id="lowHigh">Price High-Low</option>
</select>

<script>
function changedOption() {
    var sel = document.getElementsByName('pickSort');
    var sv = sel.value;
    alert(sv); 
}
</script>

Upvotes: 1

Views: 44

Answers (2)

dsgriffin
dsgriffin

Reputation: 68586

A better way of doing this without the inline stuff:

document.getElementById("chooseSort").onchange = function() {
  alert(this.value);
};

jsFiddle here.

Upvotes: 3

htatche
htatche

Reputation: 693

You need to call the function with parentheses changedOption()

 <select name="pickSort" id="chooseSort" onchange="changedOption()">

Upvotes: 0

Related Questions