0

I want to set the dropdown to the value found in sortby in the URL. I'm not sure how to get the querystring from the URL using Jquery, doesn't jquery have something built in?

URL: http://www.test.com/search.php?h=454&sortBy=books&nan=4812

<select class="SearchSortingList">
  <option value="tires">5tires and stuff</option>
  <option value="cars">5cars and stuff</option>
  <option value="books">5books and stuff</option>
  <option value="kites">5kites and stuff</option>
</select> 

This is where I'm at, I just need a bit of help please.

 // select books dropdown since the value of sortby (in url) is books
 $('select[name="SearchSortingList"]').val(JQUERY_GRABQUERYSTRINGVALUE('sortBy'));
bentley5
  • 83
  • 3
  • 9
  • this function extracts parameters from url to an array: http://jquery-howto.blogspot.de/2009/09/get-url-parameters-values-with-jquery.html – Karl Adler Jul 04 '13 at 07:23

2 Answers2

3

As far as I know there is not, but you can use a regex to extract the sortby value

var val = location.href.match(/[?&]sortBy=(.*?)[$&]/)[1];
$('.SearchSortingList').val(val);
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
3

Try:

var sortBy = window.location.href.match(/[?&]sortBy=([^&#])*/)[1];
$('select.SearchSortingList option[value="' + sortBy + '"]').prop("selected", true);

select[name="SearchSortingList"] should be select.SearchSortingList because SearchSortingList is actually a class rather than a name.

mishik
  • 9,973
  • 9
  • 45
  • 67