Description
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);
}
Explanation & Answer
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>
Review
Review
24/7 Homework Help
Stuck on a homework question? Our verified tutors can answer all questions, from basic math to advanced rocket science!
Similar Content
Related Tags
The Splendid and The Vile
by Erik Larson
The Glass Castle
by Jeannette Walls
Dune
by Frank Herbert
50 Shades of Grey
by E. L. James
Frankenstein
by Mary Shelley
The Elegant Universe
by Brian Greene
The Lord of the Flies
by William Golding
The Red Badge of Courage
by Stephen Crane
How to Win at the Sport of Business
by Mark Cuban