I’ve searched the internet and stackoverflow for how to trigger a second click function and this is what I found:
$('.elementclass').toggle(
function(){
//on odd
},
function(){
//on even
});
It’s perfect. Now according to this I try to change the URL of my document but something doesn’t work. Here is my code:
$('.orderby').toggle(
function(){
document.location.href+='?orderby='+$(this).val()+'ASC';
},
function(){
document.location.href+='?orderby='+$(this).val()+'DESC';
});
where $(this).val() will be something like name or date…
What I want to accomplish is: on first click of a button, the URL changes to http://blablabla/page.php?orderby=nameASC and then on the second click, the URL changes to http://blablabla/page.php?orderby=nameDESC.
So what’s wrong with my code?
I don’t want to refresh the page when user click on button, I just want to add some (one in this case) parameters that can take whit $_GET later but on same page.document.URL can be update whit +=?orderby=nameASC on first click and on second click +=?orderby=nameASC need to be remove and document.URL update whit +=?orderby=nameDESC.
You page changes so the script is loaded again.
You need to test what url you have
Updated to handle different orderby values
PS: document.location.href was deprecated many many years ago. Use document.URL or window.location.href instead
PPS: As MilkyWayJoe correctly stated – why bother – if the page is loaded from the server into the current page and not into an iframe or ajaxed, then set the value of the button on the server and have a normal link – that would make it work even when JS was turned off
UPDATE2
IF you use AJAX you want to do stuff like
If you want to use in-browser sorting, use one of these
http://www.tripwiremagazine.com/2012/02/jquery-filter-sort-plugins.html
and have