Solved

jQuery get selected item

Posted on 2012-04-09
2
581 Views
Last Modified: 2012-06-22
I have a form with a simple dropdown box.

I'm trying to use jQuery to get the value of the currently selected item.

I've tried,
var accountcode = $('select#accountdropdown').val();

var accountcode = $('select#accountdropdown:selected').val();

and a few other variations and no matter the change I still get only the first value in the drop down box even if it's not selected. For instance CODE1 will always be returned.



<select name="shippingdropdown" id="shippingdropdown" class="large-input">
<option value="CODE1">C1</option><
option value="CODE2">C2</option>
<option value="CODE3">C3</option>
</select>
0
Comment
Question by:JPERKS1985
2 Comments
 
LVL 16

Expert Comment

by:HagayMandel
ID: 37825641
var accountcode = $('select#accountdropdown:selected').text();
0
 
LVL 9

Accepted Solution

by:
alex_code earned 500 total points
ID: 37827049
You need to work with change function and the val() of the select box. Try the following:

$('#shippingdropdown').change(function(){

       var get_the_selected_value=$(this).val();
       alert(get_the_selected_value);

    });
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now