Add set value of select box using url param

Share this article

jQuery code snippet to add/set value of select box using a supplied url parameter.


jQuery(document).ready(function() {
	$.urlParam = function(name){
		var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
		return results[1] || 0;
	}
	//set the class
	$('select[name=cabinClass] option[value="'+$.urlParam('class').toUpperCase()+'"]').attr('selected', true);
});
Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week