3
            <div id="selected_options">
            <select onchange="test()" id="selected_options">
            <option value="0" selected>-Select-</option>
            <option value="1">Communication</option>
            <option value="2">Hardware</option>
            </select>
    </div>

i had written a function for selected value , but when i'm doing alert of selected value it showing up undefined , the function is

      function test() {
                    var get_id = document.getElementById('selected_options');
                    var result = get_id.options[get_id.selectedIndex].value;
                    alert(result);
                      }

any one please tell me what is the error?

7 Answers 7

6

You also have two id's with selected_options. As this JSFiddle would alert:

<div id="selected_options">
    <select onchange="test()" id="selected_opt">
        <option value="0" selected>-Select-</option>
        <option value="1">Communication</option>
        <option value="2">Hardware</option>
    </select>
</div>

function test() {
    var get_id = document.getElementById('selected_opt');
    console.log(get_id);
    var result = get_id.options[get_id.selectedIndex].value;
    alert(result);
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 this is the issue, same thing I came up with jsfiddle.net/loktar/qDTdn you just beat me to it :P
@Siva do you have another id='selected_options' on your DOM?
3

The error is here:

get_id.options[get_id.selectedIndex].value;

It should be

get_id.value

And it will show up the selected value :)

Comments

1

get_id[get_id.selectedIndex].value; "options" not required.

Comments

0

To get the currently selected value you can do:

function test() {
    var get_id = document.getElementById('selected_options');
    var result = get_id.value;
    alert(result);
}

Comments

0

It's probably because your div and select tags have the same id.

1 Comment

no that is not the problem when i changed also it coming as undefined
0

use jQuery its much easier:

var result = $('#selected_options option:selected').val();

and use ids only once

Comments

0

the problem in your code is the same id => 'selected_options' for both div and combobox.

ids are meant for unique element representation in a page.

you can check its live example here:
http://outsourcingnepal.com/projects/kool.htm

code:

<script>
function test() {
        var get_id = document.getElementById('selected_options');
    var result = get_id.options[get_id.selectedIndex].value;
    alert(result);
}
</script>
<h1>Outsourcing Nepal</h1>
<a href="http://www.outsourcingnepal.com">Home</a>
<div id="selected_options1">
    <select onchange="test()" id="selected_options">
            <option value="0" selected>-Select-</option>
            <option value="1">Communication</option>
            <option value="2">Hardware</option>
    </select>
</div>

Comments

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.