$.urlfilter

$.urlfilter() allows links to update URL query parameters instead of replacing them.

Example: Let’s say the following HTML is on the page /?city=NY.

<a class="urlfilter" href="?name=John">Add ?name=John to URL</a>
<script>
  $("body").urlfilter();
</script>

Clicking on any .urlfilter (trigger) in body (container) opens /?city=NY&name=John. The href= in the .urlfilter link updates the current page URL instead of replacing it.

data-mode controls the way the URL is updated by the href:

URL href default data-mode="add" data-mode="toggle" data-mode="del"
? ?x=1 ?x=1 ?x=1 ?x=1 ?
?x=1 ?x=1 ?x=1 ?x=1&x=1 ? ?
?x=1 ?y=1 ?x=1&y=1 ?x=1&y=1 ?x=1&y=1 ?x=1
?x=1 ?x=2 ?x=2 ?x=1&x=2 ?x=1&x=2 ?x=1

For example:

<li><a class="urlfilter" href="?city=NY"> Change ?city= to NY</a></li>
<li>
  <a class="urlfilter" href="?city=NY" data-mode="add"> Add ?city= to NY</a>
</li>
<li>
  <a class="urlfilter" href="?city=NY" data-mode="del">
    Remove NY from ?city=</a
  >
</li>
<li>
  <a class="urlfilter" href="?city=NY" data-mode="toggle">
    Toggle NY in ?city=</a
  >
</li>
<li>
  <a class="urlfilter" href="?city=NY" data-bs-target="pushState"
    >Change ?city= to NY using pushState</a
  >
</li>
<li>
  <a class="urlfilter" href="?city=NY" data-bs-target="#">
    Change location.hash, i.e. #?city= to NY</a
  >
</li>

This works with input, select and form elements as well.

<p><label><input type="checkbox" class="urlfilter" name="a" value="1" data-mode="toggle" data-bs-target="#"> a=1</label></p>
<p>
  <label><input type="radio" class="urlfilter" name="b" value="1" data-bs-target="#"> b=1</label>
  <label><input type="radio" class="urlfilter" name="b" value="2" data-bs-target="#"> b=2</label>
</p>
<p><label><input type="range" class="urlfilter" name="c" data-bs-target="#"> c=</label></p>
<p>
  <select name="d" class="urlfilter" data-bs-target="#">
    <option></option>
    <option>1</option>
    <option>2</option>
  </select>
</p>
<p>
  <form class="urlfilter" data-bs-target="#">
    <input name="x" placeholder="x value">
    <input name="y" placeholder="y value">
    <button name="z" value="z2" type="submit">Submit</button>
  </form>
</p>

You can target an IFrame or DOM element to change the URL:

<p>
  <a class="urlfilter" href="?city=NY" data-bs-target="iframe"
    >Change iframe URL ?city= NY</a
  >
</p>
<iframe src="?country=US"></iframe>

<p>
  <a class="urlfilter" href="?city=NY" data-bs-target=".block">
    Use AJAX to load ?city=NY into .block</a
  >
</p>
<div class="block" src="?country=US"></div>

$.urlfilter attributes

When we run $('body').urlfilter(), the body is called a “container”. It listens to events on “triggers”, like <a class=".urlfilter"...>

Triggers support these attributes:

Containers support these attributes:

$.urlfilter events