How-to: Access an Option in a Select List Using jQuery

I had to do some documentation-digging to figure this one out. If you want to be able to have access to <option>’s within an HTML <select> list and fool with their properties, here is one way to do it. Hopefully this will help others who are stuck and of course to help me remember :) I’m sure there are other ways to do this, if ya got one please put it up!

Example Use Case:

You need to change an <option> of a known ‘value’ to ‘selected’ , but you only know the <select> list ‘name” property.

Example Javascript:

$(document).ready(function()){
var yourVal = 13;
$(“select[name='yourselectName'] option[value='"+yourVal+"']“).attr(‘selected’, true);
}

About John Pencola

Hello, my name is John Pencola. I can't get enough of exploring new technologies, discussing software principals, creating programs and having fun with interface design. I will share my experiences here and hope Liquid Language adds some useful information to the vast sea that is the web.
This entry was posted in How-To, JavaScript and tagged , , , , . Bookmark the permalink.