eckstein
eckstein

Reputation: 115

bind url to checkbox value

I have this checkbox which I want to use as a language-switch:

<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 index.php file where the language of the content changes to German if you add ?lang=de or changes to french if you add ?lang=fr. Now is it possible to bind each of the URLs to one of the checkbox value? Like if the checkbox isn't checked the param ?lang=de and if checked, the param ?lang=fr will be added to the URL and let show the content in this language?

Upvotes: 3

Views: 103

Answers (1)

Sougata Bose
Sougata Bose

Reputation: 31749

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;
})

Upvotes: 1

Related Questions