I'm having a little problem with finding and displaying strings from an array. Haven't done any sort of code in years so I'm kind of rusty. My problem is this:
Say I have an array like this:
var titles = ["0","Little Mouse", "1","Shaman Disciple", "2","Accomplished Shaman", "3","Shaman", "4","Little Pixie"];
(the numbers before the titles are the title id's which are used, I just use titles[i-1] to fetch it, it's pretty important they're in that sort of order!)
And that I wanted to find every string in that array that contains "little" and display its corresponding number. I came up with this, but it won't work and I've already tried reading a bunch about loops and writing over stuff, but I just can't figure it out. I came up with this script:
var x=document.getElementById("title").value;
var xs=x.toLowerCase();
for(var i = 0; i <= titles.length; i++){
if(xs.indexOf(titles[i].toLowerCase()) != -1){
document.getElementById("command").innerHTML = "/title " + titles[i-1];
} else {
document.getElementById("command").innerHTML = "no title like that";
}
}
It's set to go on onkeyup="dostuff()" on a textfield (which I know can't be healthy or good code, but whatever), and it DOES work if you put in the full string, it just doesn't display all the matches in the array. I do get that I SHOULD use innerHTML += blahblah and not innerHTML = blahblah, BUT it just adds to the titles indefinitely! What the hell do I do
Sorry for the wall of text!