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>
<title>Setting Dropdown Value</title>
<script src=""></script>
<script type="text/javascript">
         $("#cars option[value='benz']").attr("disabled",true);
         $("#cars option[value='benz']").attr("disabled",false);
   <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>
   <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">
  • 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

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!