Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have a dropdown menu for a website and I want the selected option to be a variable in js so I can print/return it later on. Thanks in advance, I'm really new to javascript.


The JS I thought it is:
JavaScript
var P1 = document.getElementById("bammon1");
var Pokemon1 = P1.options[P1.3].text;


HTML

HTML
<label class="label">   Pokemon</label>
                <select id="bammon1">
                    <option>Please select a pokemon</option>
                        <option pokemon1=''>1) Simorgh</option>
                        <option pokemon1=''>2) --</option>
                        <option pokemon1=''>3) Flytrap</option>
                        <option pokemon1=''>4) Name Needed</option> 
                        <option pokemon1=''>5) Pandoze </option>
                        <option pokemon1=''>6) Apaflite </option>
                        <option pokemon1=''>7) Dendurus</option>
                        <option pokemon1=''>8) Bandirto</option>
                        <option pokemon1=''>9) --</option>
                        <option pokemon1=''>10) Tiflectum</option>
                        <option pokemon1=''>11) Sinder</option>
                        <option pokemon1=''>12) --</option>
                        <option pokemon1=''>13) Springfest</option>
                        <option pokemon1=''>14) Mirrage</option>
                        <option pokemon1=''>15) Skulprit</option>
                        <option pokemon1=''>16) --</option>
                        <option pokemon1=''>17) --</option>
                        <option pokemon1=''>18) Swelter</option>
                        <option pokemon1=''>19) Tundear </option>
                        <option pokemon1=''>20) Slimurk </option>
                        <option pokemon1=''>21) Sluborac</option>
                        <option pokemon1=''>22) Ogre </option>
                        <option pokemon1=''>23) Name Needed</option>
                        <option pokemon1=''>24) Name Needed</option>
                        <option pokemon1=''>25) Cychill </option>
                        <option pokemon1=''>26) Cydnidie</option>
                        <option pokemon1=''>27) Name Needed</option>
                        <option pokemon1=''>28) Name Needed</option>
                        <option pokemon1=''>29) Name Needed </option>
                        <option pokemon1=''>30) Farenheat </option>
                        <option pokemon1=''>31) Celsice</option>
                        <option pokemon1=''>32) --</option>
                        <option pokemon1=''>33) Spychic </option>
                        <option pokemon1=''>34) Murdoom </option>
                        <option pokemon1=''>35) --</option>
                        <option pokemon1=''>36) Name Needed </option>
                        <option pokemon1=''>37) Name Needed</option>
                        <option pokemon1=''>38) Name Needed </option>
                        <option pokemon1=''>39) Name Needed </option>
                        <option pokemon1=''>40) Name Needed </option>
                        <option pokemon1=''>41) Name Needed </option>
                        <option pokemon1=''>42) --</option>
                        <option pokemon1=''>43) --</option>
                        <option pokemon1=''>44) --</option>
                        <option pokemon1=''>45) --</option>
                        <option pokemon1=''>46) --</option>
                        <option pokemon1=''>47) --</option>
                        <option pokemon1=''>48) --</option>
                        <option pokemon1=''>49) --</option>
                        <option pokemon1=''>50) --</option>
                        <option pokemon1=''>51) Charidbyss </option>
                        <option pokemon1=''>52) --</option>
                        <option pokemon1=''>53) --</option>
                        <option pokemon1=''>54) --</option>
                        <option pokemon1=''>55) Faeum </option>
                        <option pokemon1=''>56) Wyvearn</option>
                        <option pokemon1=''>57) --</option>
                        <option pokemon1=''>58) --</option>
                        <option pokemon1=''>59) --</option>
                        <option pokemon1=''>60) --</option>
Posted
Updated 8-Feb-15 5:15am
v2

1 solution

To get the selected item, use the selectionIndex property:
JavaScript
var P1 = document.getElementById("bammon1");
var Pokemon1 = P1.options[P1.selectedIndex].text;

Also, I don't know why you have empty pokemon1 attributes in your option tags, but I don't think they should be there -- it's invalid HTML. If you actually want to put (and use) values in them, make the attribute name start with data- (for example, data-pokemon1). This kind of attribute is called a data attribute: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes[^]
 
Share this answer
 
Comments
Member 11436592 8-Feb-15 11:39am    
So if I want it to it me the 4th character onward it should be:
var P1 = document.getElementById("bammon1");
var Pokemon1 = P1.options[P1.3].text;
and should the options be this then? <option pokemon1='data-pokemon1'>30) Farenheat </option>
Thomas Daniels 8-Feb-15 11:42am    
No. Instead of P1.3, just use 3. And at your attributes, it's the attribute name that should start with data-, not the attribute value. And as it looks like you do not use these attributes, you can just remove them.
Member 11436592 8-Feb-15 11:55am    
okay thanks, this is my first time making a website so you've been a big help
Thomas Daniels 8-Feb-15 11:56am    
You're welcome!
Member 11436592 8-Feb-15 21:59pm    
sorry another quick question, ehre do I put 'data-'? I removes " pokemon1=''" and it doesn't work, can you tell me again how I can have, for example if '1.) Simorgh' is selected how do I get Pokemon1 = Simorgh

Because right now it only keeps the first option as the variable, do I need to reassign it after a selection is made I wonder?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900