-1

Whats wrong with this script? It resets itself and the second div never changes. I'm missing something? I'm thinking I probably need a better way to handle the variable so if someone knows of one that would be awesome. This is my jsfiddle testing script:

var lang="de";

$('#en').click(function () {
    lang="en";
});
$('#de').click(function () {
    lang="de";
});
$('#es').click(function () {
    lang="es";
});

function showtext() {
    $('#text').text(lang); 
    if (lang="en") {
        $('#cur').text(lang);
    }
    else if (lang="de") {
        $('#cur').text(lang);
    }
    else if (lang="es") {
        $('#cur').text(lang);
    }
}

showtext();

setInterval(function () {
    showtext();
}, 2000);

Demo on jsfiddle

1

3 Answers 3

4

Your function is doing assignment through =, for comparison you must use ==.

function showtext() {
    $('#text').text(lang); 
    if (lang == "en") {
        $('#cur').text(lang);
    }
    else if (lang == "de") {
        $('#cur').text(lang);
    }
    else if (lang == "es") {
        $('#cur').text(lang);
    }
}

jsFiddle Working demo

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

1 Comment

omg, I feel so dumb now, I should of seen that, Ive been racking my brain why it wasn't working lol. thanks for helping me see it!
0

Your comparisons are actually assignments try below:

$('#text').text(lang); 
    if (lang=="en"){
        $('#cur').text(lang);
    }
    else if (lang=="de"){
        $('#cur').text(lang);
    }
    else if (lang=="es"){
        $('#cur').text(lang);
    }

Comments

0

You have an if loop that does exactly nothing (and it uses the wrong comparison operator). Try this instead:

http://jsfiddle.net/Su3RC/158/

function showtext() {
    $('#text').text(lang);
    $('#cur').text(lang);
}

2 Comments

I believe he is wanting to learn/fix if-statements, rather than optimize his snippet of code.
Fair enough. I wasn't sure.

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.