Can I use onclick twice ?

Asked

Viewed 832 times

-2

I have a button and in it I need to use onclick to trigger the function (this) for javascript but I need to put sound on the button, what I do ?

  • Why don’t you use click jquery ?

  • I don’t know how to use Jquery .

  • 1

    It would be easier for you to put your little code, so the stackoverflow guys understand better

4 answers

6

You can separate the commands on onclick through the ;

<input type="button" value="click me" onclick="alert('alerta1'); alert('alerta2');" />

0

Put a function to be executed in onClick with the procedures you want... ex:

<script>
  function onClickBotao() {
    alert('faz algo');
    alert('faz outra coisa');
  }

</script>
<input type="button" value="click me" onclick="onClickBotao();" />

https://jsfiddle.net/rvrek3gs/

  • It is already like this, but in onclick I need to trigger this too, and the 2 together do not work

  • This one you need to use is to reference the object that was clicked? because it could modify the code by passing this in the call: onClickBotao(this) and accepting an argument in the function: function onClickBotao(elemento){&#xA; alert(elemento.value);&#xA; }&#xA;

0

You can invoke your function as a function:

JS:

function minhaFuncao1() {
//Fazer alguma coisa aqui
   minhaFuncao2(); // chamar função "minhaFuncao2"
}

function minhaFuncao2() {
 // tocar som
}

Html:

<button onclick="minhaFuncao1()">Click</button>

More detail: https://www.w3schools.com/js/js_function_invocation.asp

  • It would be great however as I call the sound logo in html and n in javascript understands ?

0

You can simply make the two calls you need inside your onClick, so you would have both the required method along with the method that will play the sound you want, something like:

<input type="button" value="click som" onclick="metodo1(); metodoSom();" />

Browser other questions tagged

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