Bind url to checkbox value

I have this checkbox that I want to use as a language switcher:

<label class="switch">
    <input type="checkbox" class="switch-input">
    <span class="switch-label" data-on="DE" data-off="FR"></span>
    <span class="switch-handle"></span>
</label>

      

I have one single file index.php

where the content language changes to German if you add ?lang=de

or change to French if you add ?lang=fr

. Now is it possible to bind each of the urls to one of the checkbox values? How if the checkbox was not checked with param ?lang=de

, and if this checkbox is checked, the parameter will be added to the url ?lang=fr

and display the content in that language?

+3


source to share


1 answer


Try with



var param = "lang=de";

$('.switch-input').on('click', function() {
    if($(this).is(':checked')) {
        param = "lang=" + $(this).siblings('.switch-label').data('on');
    } else {
        param = "lang=" + $(this).siblings('.switch-label').data('off');
    }
    window.location.href = window.location.href + '?' + param;
})

      

+1


source







All Articles