0

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

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Keleko
  • 245
  • 4
  • 15
  • Possible duplicate: http://stackoverflow.com/questions/13851399/javascript-if-statements-not-working – Ja͢ck Feb 21 '13 at 02:24

3 Answers3

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

Jesse
  • 8,605
  • 7
  • 47
  • 57
  • 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! – Keleko Feb 21 '13 at 02:27
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);
    }
Victor
  • 676
  • 1
  • 5
  • 17
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);
}
isherwood
  • 58,414
  • 16
  • 114
  • 157