Return one of the functions in jquery.

Asked

Viewed 48 times

1

Hello is it possible to return separately one of these two functions that are within another function ? For example depending on the situation I want to return the result of the question Function and at another time the answer Function ?

function psNF(){
    var pergunta = function(){
        alert('a');
    }
    var resposta = function(){
        alert('b');
    }
}

  • You want to return the function or just run it?

2 answers

1


You can call the function by passing a certain parameter, so there is no need to create more than one function to call the functions.

function psNF(opcao) {

  if (opcao == 1) {
    var pergunta = function() {
      alert('a');
    }
  } else {
    var resposta = function() {
      alert('b');
    }
  }
}

Remember that the parameter that defines is you. You can pass int,string,char and several other types. Just need to stay tuned at the time of the condition if to make the right treatment.

And of course at the time of calling the function, pass the parameter you want.

  • Thank you, that opened up many options for what I intend to do!

  • Good Carlos! The sky is the limit.

0

Only create a separate named function for each of them

function psNF(){
    var pergunta = function(){
        alert('a');
    }
}

function rpNF(){
    var resposta = function(){
        alert('b');
    }
}

Then assign the function call where you will use each one.

<button type="button" name="pergunta" onclick="psNF();" />

<button type="button" name="resposta" onclick="rpNF();" />

Browser other questions tagged

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