jquery select option based on value

By using jquery you can easily select an option selectd by based on that option value.

Example of Jquery select option selected based on value:

<select id="mySelect" multiple="multiple">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
  <option value="4">Fourth</option>
</select>

<script type="text/javascript">	
$(document).ready(function() {
  if (!$("#mySelect option:selected").length)
    $("#mySelect option[value='3']").attr('selected', 'selected');
});
</script>

In Dynamic Jquery you can use this code by following:

 var vatId = parseInt(data);
 $("#vatCategoryId option[value='"+vatId+"']").attr('selected', 'selected');

Rasel Ahmed

I'm a Computer Science graduate from Bangladesh. I complete my CSE in 2008. Now i am a Senior Software Engineer at Grameen Solutions Ltd. I work in Cakephp framework. I also have complete job in wordpress, joomla, titanium, Phpfox and magento.

More Posts

Follow Me:
FacebookLinkedInGoogle Plus

Related posts:

One Response to jquery select option based on value

  1. Daniel says:

    Nice Post. so helpful

Leave a Reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>