I want to search by name of any function from a search field

1

Folks ... how can I make a system where input text gets a name of function .. then it searches its name internally with the name that is in string . kind ...

In Html I do the following:

<input type="text" id="seuInput" onkeyup="keyupFunction()">

No javascript:

function keyupFunction() {
   // codigo para achar o nome em certa função
   var valor_do_campo_aqui = document.getElementById("seuinput").value;
}

For example:

I have function carro() { ... }

This "car" is the name of a certain function. You need to open it to get certain data in it.

The question refers to something almost, I repeat almost like this - Checking if value exists in an array via search field

For example, I expect to receive only a% w / o of% - whether it exists or not .

How can I use a search to find this and other functions you can create later !?

    
asked by anonymous 15.05.2018 / 04:34

2 answers

3

One way to tell if any function exists and to call it is by using typeof() :

Example:

function carro(){
   alert("A função carro existe");
}

function keyupFunction(f) {
   if(typeof(window[f]) == "function"){
      window[f]();
   }
}
Digite, por exemplo, 'carro':
<br>
<input type="text" id="seuInput" onkeyup="keyupFunction(this.value)">
  

Note: In this example, you will only find functions with a global scope.

    
15.05.2018 / 05:11
1

Well, if it will not be used for some system that requires several saved names (it would be nothing semantic to create 30 hehehe subfunctions), here it is:

//funções filhas
function funcCarro(minhaVar){
  document.getElementById('funcFilhas').innerHTML = 'O termo <strong> ' + minhaVar + '</strong> existe em nosso sistema!';
}

function funcPessoa(minhaVar){
  document.getElementById('funcFilhas').innerHTML = 'O termo <strong> ' + minhaVar + '</strong> existe em nosso sistema!';
}

//função pai
//Esta função chamará a respectiva função filha digitada no input
function funcao(minhaVar) {
    document.getElementById('output').innerHTML = '<hr><h3 style="color: blue;">Você digitou: <span style="color: red;">' + minhaVar + '</span></h3>';
  
  if(minhaVar == 'carro')
  {
    funcCarro(minhaVar);
  }
  else if(minhaVar == 'pessoa')
  {
    funcPessoa(minhaVar);
  }
  else
    {
  document.getElementById('output').innerHTML = '<hr>O termo digitado não existe em nosso sistema!';
      
document.getElementById('funcFilhas').innerHTML = '';
    }
}
<html>
<body>
<label>Busca: <input type="text" onkeyup="funcao(this.value)"></label>
<pre style="color: silver; font-size: 12px; margin: 5px 0 0 0;">Dica: Digite "carro" ou "pessoa"</pre>
<div id="output"></div>
<div id="funcFilhas"></div>
</body>
</html>

Good studies!

    
15.05.2018 / 05:11