Skip to content Skip to sidebar Skip to footer

Change Url Depending On Click

I want to add a language switcher to a specific html page. I don't need any language text only the link to another site will need to be changed depending on the click. It wouldn't

Solution 1:

HTML:

<adata-lang="Spanish">To Spanish</a><adata-lang="Russian">To Russian</a>

JavaScript:

var current = "English"
$("[data-lang]").on("click", function() {
    var lang = $(this).data("lang");
    $(".clickButton").prop("href", function(i, href) {
        return href.replace(current, lang);
    });
    current = lang;
});

DEMO:http://jsfiddle.net/a2xqL/

Solution 2:

make it easy for yourself: add custom data attribute to links;

<a data-lang="English" href="">

Than access it with jQuery by $link.data('lang'); and add it to the href.

Post a Comment for "Change Url Depending On Click"