NinjaBoy
NinjaBoy

Reputation: 3755

How to get the html select values using Javascript?

I want to know how can I get the value of the selected option whenever I change the value combobox. Here's my code:

<html>
<head>
<script type="text/javascript">
function dispOptionValue() {
 var select = document.getElementById('footballPlayers');
 alert(select.options.value);
}
</script>
</head>
<body>
<select id="footballPlayers" onchange="dispOptionValue">
 <option value="1">Ricardo Kaka</option>
 <option value="2">Cristiano Ronaldo</option>
 <option value="3">Johan Crujjf</option>
 <option value="4">Gerd Muller</option>
 <option value="5">Franz Beckenbauer</option>
</select>
</body>
</html>

I know its wrong but I don't have any idea how to start. Please help.

Upvotes: 0

Views: 1381

Answers (2)

Matteo Vinci
Matteo Vinci

Reputation: 208

You can modify your code as follow:

function dispOptionValue() 
{
     var select = document.getElementById('footballPlayers');
     alert(select.options[select.selectedIndex].value);
}

And change onChange event in dispOptionValue()

Upvotes: 1

4b0
4b0

Reputation: 22323

try:

<script type="text/javascript">
$(document).ready(function() {


$('input[type=checkbox]').each(function () {
    var select = $(this).val();
    alert(select);
});

 });
</script>

Do not forget to include js file.

Upvotes: 0

Related Questions