Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery get selected item

Posted on 2012-04-09
2
Medium Priority
?
656 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 earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

971 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