• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Set Drop Down Value using JQUery

June 3, 2014 //  by Krishna Srinivasan//  Leave a Comment

This tutorial explains how to set dropdown box value in jQuery. To display the selected drop down box value, we use following way. i.e. $(“#idName”).val();. To set particular value in the drop down box value, we use following way; $(“#idName”).val(“audi”);. The following example shows how to set drop down value, disabling and enabling of value in the drop down list. If you have any questions, please write it in the comments section.

JQuery Setting Dropdown Value Example

<!doctype html>
<head>
<title>Setting Dropdown Value</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<style>
   #cars{
      background:cyan;
   }
</style>
<script type="text/javascript">
   $(document).ready(function(){
      $("#selected_val").click(function(){
         alert($("#cars").val());
      });
      $("#my_audi").click(function(){
         $("#cars").val("audi");
      });
      $("#my_volvo").click(function(){
         $("#cars").val("volvo");
      });
      $("#disable_benz").click(function(){
         $("#cars option[value='benz']").attr("disabled",true);
      });
      $("#enable_benz").click(function(){
         $("#cars option[value='benz']").attr("disabled",false);
      });
   });
</script>
<body>
   <select id="cars">
      <option value="None" style="background-color: Gray;">--Select--</option>
      <option value="audi" style="background-color: Brown;">Audi</option>
      <option value="volvo" style="background-color: Pink;">Volvo</option>
      <option value="benz" style="background-color: Orange;">Benz</option>
   </select><br><br>
   <input type="button" value="Dispaly Selected Value" id="selected_val">
   <input type="button" value="Select Audi" id="my_audi">
   <input type="button" value="Select Volvo" id="my_volvo">
   <input type="button" value="Disable Benz" id="disable_benz">
   <input type="button" value="Enable Benz" id="enable_benz">
</body>
</html>
  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“#selected_val”).click(function(){alert($(“#cars”).val());}) line define click method which occurs when user clicks on the button, alert message will get display with specified value.
  • $(“#disable_benz”).click(function(){$(“#cars option[value=’benz’]”).attr(“disabled”,true);}) line specifies disable of particular value in the drop down list. When an element is disabled, it appears dimmed and does not respond to user input. The disabled attribute selector is a boolean attribute, where true value indicates element is disabled and false value indicates element is not disabled.
  • We have used CSS style color property to set different background color for each value in the drop down box.

JQuery Setting Dropdown Value Demo

When you run the above example, you would get the following output:

  • Run Drop Down Value Demo

JQuery Drop Down Value

Category: jQueryTag: JQuery Input

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: «jquery JQuery Text Selector Example
Next Post: JQuery slideToggle Example jquery»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact