I'm doing a jquery dropdown menu to change language on a site. As you can see in the code below I am storing languages in one array and images relating to those languages in another. When a button is clicked I want to look at the text in that button, get that text's index in the array, then use that index to add its image somewhere else.
At the moment I am getting no image at all. The variable btnText is working as I can see the result in #languageBtn, but something is not working on line 7 with indexOf. If I change line 7 to a string and not the btnText variable e.g. indexOf("Deutsch") it works fine and I get an image.
indexOf(something) only seems to work if something is not a variable but a string in quotes. Any ideas anyone?
- languageArr = new Array("English", "Español", "Deutsch", "Français");
- languageFlagArr = new Array("flag_en.gif", "flag_es.gif", "flag_de.gif", "flag_fr.gif");
-
- $('.languageBtns:not(#languageBtn)').mouseup(function(e){
- var btnText = $(e.target).text();
- var indexOf = languageArr.indexOf(btnText);
- $('#languageBtn').text(btnText);
- $('<img>').addClass('langFlag').attr({'src': 'img/' + languageFlagArr[indexOf], 'height': '10', 'width': '16', 'border': '0'}).appendTo('#languageBtn');
- $('#languageBtnsBg').hide(200);
- });