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

Asked

Viewed 126 times

1

Guys... how can I make a system in which the input text gets a name from function.. then he searches his name internally with the name that is on string. guy...

In HTML I do the following:

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

In 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 the function carro() { ... }

this "car" is the name of a certain function. And I need to open it to call certain data on 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 hope to receive only one alert - whether it exists or not.

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

  • is something like this? > https://codepen.io/alexlupoz/pen/ELpevJ

  • I will elaborate one with HTML Inner and put as an answer, for posteriority that also end up finding your post by chance and want to remedy such doubts. A moment.

  • Although the answers you have are good and answer what you asked, they are hardly what you want to do. If you show how and where you will use them, it will be easier to advise an alternative.

  • But how do these researches differ ? Aren’t all researches generalizable to use a single function ? This is something that with the little context given in the question is not possible to answer

  • @Isac Well, if I were going to post a lot of code or say a little bit of detail, I would certainly get a Dowvote because it wouldn’t be the focus of the context and the ethics of the site. Here on pt.stackoverflow.com aims to elaborate question in isolation for each one of which is the problem, which in my case was that of the issue. I couldn’t say too many lines and/or post too many chunky codes for the question. Maybe someone or some user would get confused and would ask questions in the commissaries asking for more information. So I just posted and asked in isolation what I couldn’t do.

  • Yes I understand and I agree that doing so on this issue would distort it. I’m just trying to warn you that you are most likely in a XY problem and so you should rethink a little bit the way you’re trying to solve the problem.

Show 1 more comment

2 answers

3


One way to know if any function exists and 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)">

Obs.: in this example, you will only find functions with global scope.

  • @Diego Henrique, the DVD solution is the semantic solution you need. What I gave was just a simple example of syntax. I believe that he deserves the right choice of answer, because it will suit his project better. There is a way to change, no?

  • @Alexlupóz Your request is an order boss, I just follow instinct on my data votes who first respond promptly takes. But I will reverse the Vow as you asked

  • Thank you. Semantic solutions are always the best solution.

1

Well, if you’re not going to use for some system that requires several saved names (it wouldn’t be anything 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.