Triggering a javascript / JQuery function when a particular option is chosen from a select dropdown

Posted on 2014-01-09
Last Modified: 2014-01-09
If I have a web form that contains a select dropdown in that has an ID attribute of "shipping_state":

<select name="shipping_state">

Open in new window

How might I customize that form so that when a person chooses the state of Florida from that dropdown ..

<option value="FL">

Open in new window

.. a custom javascript function gets triggered?

Ideally .. I would prefer a solution that do NOT require that I modify the form itself.  

I am open to solutions that require JQuery if that is the best approach.  Otherwise, plain old javascript will do.

Thanks in advance.
- Yvan
Question by:egoselfaxis
LVL 58

Expert Comment

ID: 39768681
     if($(" :selected",this).val()=="FL"){
     // your function here
LVL 52

Accepted Solution

Scott Fell,  EE MVE earned 500 total points
ID: 39768700
Here it is in jquery,output

You said it has an id of shipping_state but your code shows it as a name.  My code below targets the name.  If you want to target the id change

$('select[name="shipping_state"]').change(function() {


$('select#shipping_state').change(function() {
<!DOCTYPE html>
<link href="" rel="stylesheet" type="text/css" />
<script src=""></script>
<script src=""></script>
<meta charset=utf-8 />
    $(function() {
$('select[name="shipping_state"]').change(function() {
    var s=$(this).val();
  if (s==='FL'){
     alert('You selected FL');
<title>JS Bin</title>
  <select name="shipping_state">
    <option value="NY">NY</option>
    <option value="FL">FL</option>
    <option value="KY">KY</option>

Open in new window


Author Closing Comment

ID: 39768705
Thank you!

- yg

Expert Comment

ID: 39768720
Here is an alert for Florida as index #2

<html xmlns="">
<head runat="server">

     function test() {
         d = document.getElementById("select_id").value;
         if (d==2) {alert(d)};


    <form id="form1" runat="server">
            <select onchange="test()" id="select_id">
            <option value="0">-Select-</option>
            <option value="1">Communication 1</option>
            <option value="2">Florida 2</option>
            <option value="3">Communication 3</option>


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript: Issue with onClick 5 34
Error in JQuery 5 36
compact pure CSS Read More Toggle 4 19
what is the difference between call and apply 1 25
This article discusses how to create an extensible mechanism for linked drop downs.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

919 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

14 Experts available now in Live!

Get 1:1 Help Now