I’m trying to display text that matches user input. It displays the result when user enter first character e.g ‘a’ but nothing happens when second character is entered e.g ‘as’. So I want Baseball and Fast to be displayed when user enters as.
// JavaScript Document
s1= new String()
s2= new String()
var myArray = new Array();
myArray[0] = "Football";
myArray[1] = "Baseball";
myArray[2] = "Cricket";
myArray[3] = "Fast";
function test() // called onkeyup() event
{
s1 = document.getElementById('filter').value;
s2 = s2+s1;
arraysearch();
}
function arraysearch()
{
for(i=0; i<myArray.length; i++)
{
if (myArray[i].indexOf(s2) != -1) // also tried using match method
{
alert('Matched');
document.getElementById('placeholder').innerHTML += myArray[i] + "<br/>";
}
}
}
Tried this out, and i implemented a solution to help you. You can use regex to better compare and match strings. It uses jquery just for dom manipulation, but thats about it. You can go ahead and edit it. Hope it helps.
http://jsfiddle.net/cy4QH/2/