How To Get The Selected Index Of A Drop Down April 01, 2024 Post a Comment I have a normal dropdown which I want to get the currently selected index and put that in a variable. Jquery or javascript. Jquery perfered. Solution 1: $("select[name='CCards'] option:selected") should do the trickSee jQuery documentation for more detail: http://api.jquery.com/selected-selector/UPDATE: if you need the index of the selected option, you need to use the .index() jquery method:$("select[name='CCards'] option:selected").index() CopySolution 2: This will get the index of the selected option on change:$('select').change(function(){ console.log($('option:selected',this).index()); });Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectname="CCards"><optionvalue="0">Select Saved Payment Method:</option><optionvalue="1846">test xxxx1234</option><optionvalue="1962">test2 xxxx3456</option></select>CopySolution 3: If you are actually looking for the index number (and not the value) of the selected option then it would bedocument.forms[0].elements["CCards"].selectedIndex/* You may need to change document.forms[0] to reference the correct form */Copyor using jQuery$('select[name="CCards"]')[0].selectedIndexCopySolution 4: the actual index is available as a property of the select element.var sel = document.getElementById('CCards'); alert(sel.selectedIndex); Copyyou can use the index to get to the selection option, where you can pull the text and value.var opt = sel.options[sel.selectedIndex]; alert(opt.text); alert(opt.value); CopySolution 5: <selectname="CCards"id="ccards"><optionvalue="0">Select Saved Payment Method:</option><optionvalue="1846">test xxxx1234</option><optionvalue="1962">test2 xxxx3456</option></select><scripttype="text/javascript">/** Jquery **/var selectedValue = $('#ccards').val(); //** Regular Javascript **/var selectedValue2 = document.getElementById('ccards').value; </script>Copy Share Post a Comment for "How To Get The Selected Index Of A Drop Down" Top Question Activating Onbeforeunload Only When Field Values Have Changed What I'm trying to achieve is to Warn the user of unsav… Onpropertychange For A Textbox In Firefox? How to handle the onpropertychange for a textbox in Firefox… Animate A Div Box With AngularJS How can I animate a div-Box in AngularJS? I've tried se… Jquery UI Add Class With Animation Does't Work See my jsfiddle (apologies for putting the javascript into … AmCharts Pie - How To Get Slice To Pull Out On RollOverSlice? I am having a lot of difficulties navigating through amChar… I Need To Check A JavaScript Array To See If There Are Any Duplicate Values. I have an array var a =['color', 'radius',… How To Eliminate Unused Arguments In An Opacity Fade? element is called but never used, they are just passed back… How Can I Attach Prototype To Object Literals In JS I am trying to understand JavaScripts Prototypal nature and… Add Extra Divs To The Dom Jquery ( Toogle Click More Function) this is my first post in this website and I just wonder if … URIError: Failed To Decode Param '///%PUBLIC_URL%/manifest.json' I am trying to revive an old React app that I coded a year … December 2024 (1) November 2024 (38) October 2024 (65) September 2024 (22) August 2024 (370) July 2024 (342) June 2024 (693) May 2024 (1296) April 2024 (799) March 2024 (1548) February 2024 (1677) January 2024 (1305) December 2023 (1285) November 2023 (366) October 2023 (580) September 2023 (320) August 2023 (332) July 2023 (278) June 2023 (364) May 2023 (203) April 2023 (137) March 2023 (131) February 2023 (189) January 2023 (267) December 2022 (133) November 2022 (231) October 2022 (171) September 2022 (167) August 2022 (488) July 2022 (288) June 2022 (299) Menu Halaman Statis Beranda © 2022 - JavaScript Apprenticeships