0

I need to access the value selected from a drop down list using Javascript. But every time I get 'null' as the answer though a list item is selected.

My HTML page:

<select class="mySelect">
    <option value="st1" selected="selected">Create new Stream</option>
    <option value="st1">Stream 1</option>
    <option value="st2">Stream 2</option>
    <option value="st3">Stream 3</option>
    <option value="st4">Stream 4</option>
</select>

<input type="button" value="show attributes" class="panel-button-attr" onclick="choice()">

When the above button is clicked, the selected value should be alerted to the user. So in my Javascript function:

function choice() {

    var choice=document.getElementById("mySelect");
    alert(choice);
    var strUser = choice.options[choice.selectedIndex].text;
    alert(strUser.toString());

}

Here, I've tried to use the first alert to check if any selected list item is identified correctly. But, at this point, it displays null and the strUsr line doesn't run at all.

I know this is actually a trivial task but am finding it hard to figure this inconsistency.

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
  • If *getElementById* returns null it means it didn't find the element. – RobG Jun 14 '16 at 06:26
  • 2
    you are trying to access the element with id "mySelect", but you have given "mySelect" as a class and not as an ID in your HTML. – Guillaume Jun 14 '16 at 06:26
  • @SpeedyNinja—no, simple coding error. – RobG Jun 14 '16 at 06:27
  • 1
    Also, favor the use of console.log over alert to enjoy a better developer experience. – Delapouite Jun 14 '16 at 06:28
  • Thanks for all the speedy replies guys. My bad, been working with this project for hours and even the simple things seem to be hidden. Changed it to ID and it works. – Nayantara Jeyaraj Jun 14 '16 at 06:34
  • I would just like to know, is there a way to display which element was selected without using a button. i.e Call the function choice() with every change in option – Nayantara Jeyaraj Jun 14 '16 at 06:35

6 Answers6

3

Please change your HTML element attribute.

You've mentioned 'mySelect' as class and in JS, you are calling it with ID reference.

Charan Kumar
  • 553
  • 2
  • 13
1

You have to specify id of select element

<select class="mySelect" id="mySelect">
Manish Parakhiya
  • 3,732
  • 3
  • 22
  • 25
0

follow the code:

you need to give id to dropdown because you try to get data by id so...

<select id="mySelect" class="mySelect">
   <option value="st1" selected="selected">Create new Stream</option>
   <option value="st1">Stream 1</option>
   <option value="st2">Stream 2</option>
   <option value="st3">Stream 3</option>
   <option value="st4">Stream 4</option>
</select>

I hope this will solve your issue....

Thanks...

0
var str = "";
$( "select option:selected" ).each(function() {
  str += $( this ).text() + " ";
});
Phong Nguyen
  • 173
  • 3
  • 15
  • Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone Jun 14 '16 at 06:52
0

plunker: https://plnkr.co/edit/Q7yyVvUTaLYvPaDW5j7G?p=preview

    <select id="mySelect" class="mySelect" >
      <option value="st1" selected="selected">Create new Stream</option>
      <option value="st1">Stream 1</option>
      <option value="st2">Stream 2</option>
      <option value="st3">Stream 3</option>
      <option value="st4">Stream 4</option>
    </select>


function choice() {

    var choice=document.getElementById("mySelect");
    alert(choice.value);  // get value
    var strUser = choice.options[choice.selectedIndex].text;
    alert(strUser.toString());
}
Priyesh Kumar
  • 2,837
  • 1
  • 15
  • 29
0

You didn't have id in your html ,so I try by class name..

function choice() {
    var choice=document.getElementsByClassName("mySelect");
    var strUser = choice[0].options[choice[0].selectedIndex].text;
    alert(strUser.toString());            
}
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52