0

I would like to add checked checkbox values as url parameter to my link. This is what I have amongst others:

<input id="box1" type="checkbox" class="checkbox" value="bx1=1"> <label for="box1"></label>
<input id="box2" type="checkbox" class="checkbox" value="bx2=1"> <label for="box1"></label>

<script>
    $(".checkbox").on("change", function() {
        var values = [];
        $('.checkbox:checked').each(function() {
            var result = $(this).val();
            values.push(result);
        });
        var key = $(".link").html(values.join("&"));
        document.write('<a href="http://www.example.com/test.html?' + key + '">Open here</a>');
    });
</script>

The expected result is "http://www.example.com/test.html?bx1=1&bx2=1". I hope someone can help me with this.

Thanks for these lines:

<input id="box1" type="checkbox" class="checkbox" value="bx1=1"> <label for="box1"></label>
<input id="box2" type="checkbox" class="checkbox" value="bx2=1"> <label for="box1"></label>
<a class="link" href="http://www.example.com/test.html">Open here</a>

<script>
    $(".checkbox").on("change", function() {
        console.log('fzef');
        var values = [];
        $('.checkbox:checked').each(function() {
            var result = $(this).val();
            values.push(result);
        });
        $(".link").attr('href', 'http://www.example.com/test.html?' + values.join("&"));
    });

    // Init link on page load
    $(".checkbox").trigger("change");
</script>

I get a &on& between my values and it does not work on IE. Any idea? Thanks!

5
  • 2
    Document.write()?? However, what's your expected result? And what is the error you have? Commented Nov 19, 2016 at 13:08
  • Sorry, I forgot to put the url in here... Commented Nov 19, 2016 at 13:11
  • That's because you setup values as an array so the "pasted" values are basically an Object and you need to extract the values from that object... Commented Nov 19, 2016 at 13:15
  • What is .link? Your 'key' var is defined on wrong way.... Commented Nov 19, 2016 at 13:19
  • 2
    document.write() will close the existing document and begin writing a new one on the first checkbox click and then you'll never be able to click another one or uncheck the first one. Commented Nov 19, 2016 at 13:20

4 Answers 4

1

You can do like this:

<input id="box1" type="checkbox" class="checkbox" value="bx1=1"> <label for="box1"></label>
<input id="box2" type="checkbox" class="checkbox" value="bx2=1"> <label for="box1"></label>
<a class="link" href="http://www.example.com/test.html">Open here</a>

<script>
    $(".checkbox").on("change", function() {console.log('fzef');
        var values = [];
        $('.checkbox:checked').each(function() {
            var result = $(this).val();
            values.push(result);
        });
        $(".link").attr('href', 'http://www.example.com/test.html?' + values.join("&"));
    });

    // Init link on page load
    $(".checkbox").trigger("change");
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you very much for this and all other suggestions! This works great!
When I hit the BACK button in my browser, the checked checkboxes are still checked, but the link does not contain the values. Any chance/idea to update the link for this case?
See my edit, you just need to force the change event on page load.
Great! Thanks for your help!
Sorry, I have another problem. Using this with my checkboxes pushes the value of the checkbox currently clicked and the value of the previously clicked checkbox. Any idea?
|
1

Wrap the checkboxes in a form, give them names and let jQuery do the work with .serialize():

$(function() {
  $("form").on("change", function() {
    var href = "http://www.example.com/test.html",
        params = $(this).serialize();

    if (params.length > 0) {
      href += "?" + params;
    }

    console.log(href);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input id="box1" name="bx1" type="checkbox" class="checkbox" value="1" />bx1
  <input id="box2" name="bx2" type="checkbox" class="checkbox" value="1" />bx2
</form>

Comments

0

The correction you need to do is in the line
var key = $(".link").html(values.join("&"));

You need to change it to
var key = values.join("&");

Why?
Because there is no .link selector in your code.

Comments

-1

try like this: you need with document.write()

 document.write('<a href="http://www.example.com/test.html?' + values.join('&') + '">http://www.example.com/test.html?' + values.join('&')+'</a>');

OR

$("button").on("click", function() {
        var values = [];
        $('.checkbox:checked').each(function() {
           var  result = $(this).val();
            values.push(result);
        });
   
    
  
  $('#url').attr('href','http://www.example.com/test.html?' + values.join('&') );
   $('#url').html('http://www.example.com/test.html?' + values.join('&'))
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="box1" type="checkbox" class="checkbox" value="bx1=1"> <label for="box1">first</label>
<input id="box2" type="checkbox" class="checkbox" value="bx2=1"> <label for="box1">second</label>
<button >Go</button>
<a id="url"></a>

4 Comments

document.write() will close the existing document and begin writing a new one on the first checkbox click and then you'll never be able to click another one or uncheck the first one.
but OP not mention with use with same page.He also write with document.write()
The OP's use of document.write() was wrong. It was just his attempt at building the string, but you can't use it with callbacks unless you intentionally want to write a new document, which is clearly not the intention here.
i am also use just print the result. I was updated with my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.