Fetching the selected value of a 'select' element in JavaScript.

Programming
Tutor: None Selected Time limit: 3 Days

I'm currently trying to fetch a value from a 'select' element using JavaScript/JQuery, but for some reason I'm encountering some errors, so I'm obviously not doing it right!

<select id="obejectlistid">

<option selectedID="square">Square </option>

<option selectedID="circle">Circle</option>

<option selectedID="triangle">Triangle</option>

</select>


This is the HTML code I'm using, and the JavaScript code is below. I'm wondering if there's any way to fetch the selected value in a select element (drop down list) using JavaScript/JQuery?

function onAddNewShape() {

var object_list = document.getElementById("objectlist");

var shape = object_list.options[object_list.selectedIndex].value;

alert(shape);

}


Nov 30th, -0001
Hi here is the working solution for you.

<html>
<head>
<script type="text/javascript">
function onAddNewShape() {


var get_id = document.getElementById("objectlist");
var result = get_id.options[get_id.selectedIndex].value;
alert(result);
}
</script>
</head>
<body>
<select id="objectlist" onchange="onAddNewShape

()">
<option selectedID="square">Square </option>
<option selectedID="circle">Circle</option>
<option selectedID="triangle">Triangle</option>
</select>
</body>
</html>
Jun 28th, 2013

Did you know? You can earn $20 for every friend you invite to Studypool!
Click here to
Refer a Friend
...
Nov 30th, -0001
...
Nov 30th, -0001
Dec 10th, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer