0

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.

2 Answers 2

1

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()

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

2 Comments

Thanks so much. How about if I want to get the content of the option how am I gonna do that?
Ah ok I got it already I just have to do select.options[select.selectedIndex].innerHTML
0

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.

1 Comment

Thanks I'll consider that later. For now I want to use native javascript. I will learn jQuery later on.

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.