Skip to content

Instantly share code, notes, and snippets.

@NKid
Created March 13, 2013 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NKid/382cdd941b4d485bfebe to your computer and use it in GitHub Desktop.
Save NKid/382cdd941b4d485bfebe to your computer and use it in GitHub Desktop.
Lazy Option
function findKeyword(event) {
var strKeyword = prompt('Input the Keyword what you want to search.');
var opts = this.options;
var matchIndex = [];
for(var i = 0; i < opts.length; i++) {
if(opts[i].text.indexOf(strKeyword) != -1) matchIndex.push(i);
}
switch(matchIndex.length) {
case 0:
alert('Not found.');
break;
case 1:
this.options[matchIndex[0]].selected = true;
break;
default:
var tmp = 'Found too many keywords.\nPlz choose below options.\n';
for(var i = 0; i < matchIndex.length; i++) {
tmp += '[' + i + '] ' + this.options[matchIndex[i]].text + '\n';
}
var index = prompt(tmp);
this.options[matchIndex[index]].selected = true;
}
}
var selectEles = document.getElementsByTagName('select');
for(var i = 0; i < selectEles.length; i++)
selectEles[i].addEventListener('focus', findKeyword, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment