I’m taking input from user and then displaying the matching results. So if user types ‘a’, the following text appears:
Football
Baseball
Fast
If user types s after a (‘as’). The following text is displayed Football
Baseball
Fast
Baseball
Fast.
But I want to remove the first 3 results when user types 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 on onkeyup()
{
s1 = document.getElementById('filter').value;
var myRegex = new RegExp((s1),"ig");
arraysearch(myRegex);
}
function arraysearch(myRegex)
{
for(i=0; i<myArray.length; i++)
{
if (myArray[i].match(myRegex))
{
document.getElementById('placeholder').innerHTML += myArray[i] + "<br/>";
}
}
}
Add
document.getElementById('placeholder').innerHTML="";as first line to your functionarraysearch(myRegex)