Return to Snippet

Revision: 47217
at June 2, 2011 23:00 by jquery4u


Initial Code
/* This function sets the combobox with the value after "like" inside the url */
(function($) {
//get the url variables and set the combo box
var comboBox = $(location).attr('href');
comboBox = decodeURIComponent(comboBox);  //decode url string
comboBox = comboBox.replace(/"/g, '');    //replace quotes
var urlArray = comboBox.split("+");             //get params
//the param we're looking for is after "like"
comboBox = urlArray[jQuery.inArray("like", urlArray)+1];
$('#combobox-id > option').each(function(index) {
    //alert($(this).text() + ' ' + $(this).val());
    console.log(index + " " + $(this).attr('value'));
    if ($(this).attr('value') === comboBox) {
        comboBox = index;
    }
});
$('#combobox-id').attr('selectedIndex', jQuery.inArray(comboBox, urlArray));

Initial URL
http://www.jquery4u.com/snippets/dynamically-combobox-value-url/

Initial Description

                                

Initial Title
jQuery Dynamically Combobox Value Based on URL

Initial Tags
jquery

Initial Language
JavaScript