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> Link to comment https://forums.phpfreaks.com/topic/120150-id-of-selected-select-box-item/ 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 Link to comment https://forums.phpfreaks.com/topic/120150-id-of-selected-select-box-item/#findComment-619016 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. Link to comment https://forums.phpfreaks.com/topic/120150-id-of-selected-select-box-item/#findComment-619679 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! Link to comment https://forums.phpfreaks.com/topic/120150-id-of-selected-select-box-item/#findComment-619707 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. Link to comment https://forums.phpfreaks.com/topic/120150-id-of-selected-select-box-item/#findComment-619827 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 Link to comment https://forums.phpfreaks.com/topic/120150-id-of-selected-select-box-item/#findComment-621211 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. Link to comment https://forums.phpfreaks.com/topic/120150-id-of-selected-select-box-item/#findComment-621797 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... Link to comment https://forums.phpfreaks.com/topic/120150-id-of-selected-select-box-item/#findComment-622117 Share on other sites More sharing options...
JasonLewis Posted August 22, 2008 Share Posted August 22, 2008 Worked for me. ??? Oh well. Link to comment https://forums.phpfreaks.com/topic/120150-id-of-selected-select-box-item/#findComment-622652 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.