merlin
merlin

Reputation: 2917

How to dynamically add parameters to every URL with jQuery Mobile?

I would like to add filtering to a "select category" page. E.g. "show only results with photos". The page consists out of multiple jQuery pages that allow a selection of the category within 3 levels. On the top I would like to add the form fields which should alter the href URL inside the pages upon select.

e.g.: enter image description here

Is it possible to alter the URL of every single a href tag and not just one of them?

Upvotes: 0

Views: 492

Answers (1)

Darren Wainwright
Darren Wainwright

Reputation: 30727

So something like this?

And this is just pseudo code - haven't tested it

Essentially, we use the a selector to get all the anchors.

We then call on jquery's each function.

And within the function we read the links ($(this)) href and set it to the same + a query string.

This is just a very short example. You might need to do various checks.

$('a').each(function(){
  $(this).attr('href', $(this).attr('href') + '?my=qstring');
});

Little update

OP wanted to know how to put this in a function to call at anytime.

Something like:

function updateUrls(){
  // you might want to target urls in a given div, so something like
  $('#myULOfUrls a').each(function(){
    $(this).attr('href', $(this).attr('href') + '?my=qstring');
  });
}

Then in your form-change, you simple call updateUrls()

Upvotes: 1

Related Questions