The Little Guy Posted August 18, 2008 Share Posted August 18, 2008 How do I get the id of a selected item in a select box? so if AT&T is selected, how do I get the id: att <select id="mPhone"> <option id="none">Select A Provider...</option> <option id="att">AT&T</option> <option id="alltel">Alltel</option> <option id="boostMobile">Boost Mobile</option> <option id="Cingular">Cingular</option> <option id="cti">CTI</option> <option id="nextel">Nextel</option> <option id="sprint">Sprint</option> <option id="tMobile">T-Mobile</option> <option id="verizon">Verizon</option> <option id="verginMobile">Virgin Mobile</option> </select> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 18, 2008 Share Posted August 18, 2008 Try this out: document.getElementById('mPhone').options[document.getElementById('mPhone').selectedIndex].id Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 19, 2008 Author Share Posted August 19, 2008 nope, that doesn't work, it just shows the number, 1, 2, 3, etc. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 19, 2008 Author Share Posted August 19, 2008 modified it, and it works! Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 19, 2008 Share Posted August 19, 2008 It might be helpful for others if you posted the solutino (and marked the thread solved). I'm also curious why you are using IDs on the options and not just using a value for them. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 20, 2008 Author Share Posted August 20, 2008 From: document.getElementById('mPhone').options[document.getElementById('mPhone').selectedIndex].id To: var phone = document.getElementById('mPhone'); var selIndex = phone.selectedIndex; var provider = phone.options[selIndex].id Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 21, 2008 Share Posted August 21, 2008 That's exactly the same thing, but on separate lines. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 21, 2008 Author Share Posted August 21, 2008 I know, but it worked, and you version didn't for some reason... Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 22, 2008 Share Posted August 22, 2008 Worked for me. ??? Oh well. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.