jQuery Dynamically Combobox Value Based on URL


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  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));

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.