0

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 make much sense here to create three html pages just to deal with some link change but my jquery skills are not that great.

The destination structure looks like this:

<ul>
<li><a  class="clickButton" href="folder1/firstLink/Languages/English/folder/index.html">Index Link</a></li>
<li><a  class="clickButton" href="../folder2/secondLink/Languages/English/folder/index.html">A different Link </a></li>
<li><a  class="clickButton" href="../../folder3/Languages/English/folder/index.html">Another different link</a></li>

<ul>

<ul>
<li><a  class="clickButton" href="folder1/firstLink/Languages/Spanish/folder/index.html">Index Link</a></li>
...
<ul>

As you can see I would only need to replace the language name ("English") with Spanish in order to make the link work. So I was thinking that the user clicks on the according flag and the url string changes accordingly.

I would be happy about some example how to switch the language name

2 Answers 2

1

HTML:

<a data-lang="Spanish">To Spanish</a>
<a data-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/

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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.