My code uses RegExp to search through an array of strings based on user input:
Clinica.prototype.pesquisarDoente = function ()
{
var exp = document.getElementById("pesquisaInput").value;
var lista = document.getElementById("listaDoentes");
if (exp)
{
while (lista.firstChild)
lista.removeChild(lista.firstChild);
var patt = new RegExp(exp);
var lenght = this.doentes.length
for ( i = 0; i < length; i++)
{
if (patt.test(this.doentes[i].nome))
{
var option = document.createElement("option");
option.appendChild(document.createTextNode(this.doentes[i].toString()));
lista.appendChild(option);
}
}
}
}
This functionality is called within an event:
var buttonPesquisa = document.createElement("input");
buttonPesquisa.type = "submit";
buttonPesquisa.value = "Search";
buttonPesquisa.addEventListener('click', function () { cl.pesquisarDoente(this); });
However, when I click the button, it clears the select list and refreshes the page. Why does that happen?