Is there any way to make this JavaScript tab completion script more efficient?
Posted
by Saladin Akara
on Stack Overflow
See other posts from Stack Overflow
or by Saladin Akara
Published on 2010-05-05T01:28:23Z
Indexed on
2010/05/05
1:38 UTC
Read the original article
Hit count: 190
This code is to be integrated into an AJAX Chat system to enable a tab auto-completion of user names:
var usernames = new Array();
usernames[0] = "Saladin";
usernames[1] = "Jyllaby";
usernames[2] = "CadaverKindler";
usernames[3] = "qbsuperstar03";
var text = "Text and something else q";
// Start of the script to be imported
var searchTerm = text.slice(text.lastIndexOf(" ") + 1);
var i;
for(i = 0; i < usernames.length && usernames[i].substr(0,searchTerm.length) != searchTerm; i++);
// End of the script to be imported
document.write(usernames[i]);
A couple of notes to be made: The array of usernames and the text variable would both be loaded from the chat itself via AJAX (which, unfortunately, I don't know), and the final output will be handled by AJAX as well.
Is there a more efficient way to do this?
Also, any tips on how to handle multiple instances of the searchTerm being found?
© Stack Overflow or respective owner