How To Get The Selected Index Of A Drop Down August 20, 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 Shiny App : Disable Downloadbutton My shiny app produces some files that user can download. I … Capture The Events Of 2 Or More Keyboards In JavaScript/Web Browser I am writing some test applications that perform simple key… D3 - Add A Raw Symbol I have a D3 code. I want to add a svg symbol 'as is… Read The Source Of A Script Tag From The Cache In Firefox I'm adding some error reporting to my application. I wa… If Decimal Value, Convert To Two Decimals AND Dot Separated Value To Comma Separated I currently have values that look like the following: 30 32… HTML Transient Modal Window We have a legacy web application. At various places it open… How To Cancel User Upload In Formidable (Node.js)? I have been working on this problem for two days now, and I… Bootstrap Validation On Dynamically Adding Multiple Fields I am using bootstrap v3.1.1 and I want to validate a form w… If The User Already Logged In Then The First Tab Will Hide And Directly Jump To The Second Tab I have created a tab section without using Jquery UI. Now I… Is There Any Kind Of JQuery.browser Fallback? As jQuery.browser has been removed since 1.9 I am getting a… December 2024 (1) November 2024 (36) October 2024 (71) September 2024 (24) August 2024 (364) July 2024 (343) June 2024 (701) May 2024 (1291) April 2024 (807) March 2024 (1536) February 2024 (1692) January 2024 (1329) December 2023 (1351) November 2023 (386) October 2023 (544) September 2023 (294) August 2023 (358) July 2023 (277) June 2023 (368) May 2023 (222) April 2023 (139) March 2023 (143) February 2023 (185) January 2023 (263) December 2022 (131) November 2022 (240) October 2022 (147) September 2022 (166) August 2022 (496) July 2022 (315) June 2022 (295) Menu Halaman Statis Beranda © 2022 - javascript html php