Posted By

jquery4u on 06/02/11


Tagged

jquery


Versions (?)

jQuery Dynamically Combobox Value Based on URL


 / Published in: JavaScript
 

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

  1. /* This function sets the combobox with the value after "like" inside the url */
  2. (function($) {
  3. //get the url variables and set the combo box
  4. var comboBox = $(location).attr('href');
  5. comboBox = decodeURIComponent(comboBox); //decode url string
  6. comboBox = comboBox.replace(/"/g, ''); //replace quotes
  7. var urlArray = comboBox.split("+"); //get params
  8. //the param we're looking for is after "like"
  9. comboBox = urlArray[jQuery.inArray("like", urlArray)+1];
  10. $('#combobox-id > option').each(function(index) {
  11. //alert($(this).text() + ' ' + $(this).val());
  12. console.log(index + " " + $(this).attr('value'));
  13. if ($(this).attr('value') === comboBox) {
  14. comboBox = index;
  15. }
  16. });
  17. $('#combobox-id').attr('selectedIndex', jQuery.inArray(comboBox, urlArray));

Report this snippet  

You need to login to post a comment.