Question

I have a link which needs to change, depending on what checkboxes are active. While the default link might be "/static_part&variable=one,two,three" for all 3 checkboxes active, when checkbox one and three are checked the link should be "/static_part&variable=one,three". I go this to work, except the static part of the url is also removed, where it becomes "one,three" without the needed static part "/static_part&variable=".

Why is this not working?

 $('.check:checked').each(function () {
     "/static_part&variable=" + links.push($(this).data('link')); //get links
 });

Demo: http://jsfiddle.net/zPpXS/

JS

$('.check').change(function () {
      var links = []; //array for links

      $('.check:checked').each(function () {
          links.push($(this).data('link')); //get links
      });

      $(".link").attr("href", links.join(",")); // change link
  });

HTML

<label><input class="check" type="checkbox" checked data-link="one"><span class="checkstate">item one</span></label>
<br />
<label><input class="check" type="checkbox" checked data-link="two"><span class="checkstate">item two</span></label>
<br />
<label><input class="check" type="checkbox" checked data-link="three"<span class="checkstate">item three</span></label>
<br />
<a class="link" href="/static_part&variable=one,two,three">click link</a><br />
Was it helpful?

Solution

You're missing the static part in your link building.

In your Javascript, change

$(".link").attr("href", links.join(","));

to

$(".link").attr("href", "static_part?variable=" + links.join(","));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top