How To Set Drop Down Value using JQUery

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:

JQuery Drop Down Value

Comments

comments

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.

Speak Your Mind

*