How to call functions with the onclick event? -Javascript

Question:

<script>
        function pausaplay(){
  document.getElementById('demo45').play();
  document.getElementById('demo14').pause();
}
</script>
<input value="Mirar Havana" onclick="if(this.parentNode.getElementsByTagName('div')[0].style.display != ''){this.parentNode.getElementsByTagName('div')[0].style.display = '';this.value = 'Mirar Havana';}else{this.parentNode.getElementsByTagName('div')[0].style.display = 'none'; this.value = 'Mirar Rockabye';}" type="button">

I have tried it like this:

<script>
function pausaplay(){
if(this.parentNode.getElementsByTagName('div')[0].style.display != ''){this.parentNode.getElementsByTagName('div')[0].style.display = '';this.value = 'Mirar Havana';}else{this.parentNode.getElementsByTagName('div')[0].style.display = 'none'; this.value = 'Mirar Rockabye';}
  document.getElementById('demo45').play();
  document.getElementById('demo14').pause();
}
</script>

<input value="Mirar Havana" onclick="pausaplay()" type="button">

but I don't think so, I'm learning javascript until now, that's why I have many doubts… thanks for your respect

Answer:

If I understood correctly, the way you did it is correct.

<input value="Mirar Havana" onclick="pausaplay();" type="button">

Inside the onclick you add the name of your function to execute, obviously you will have to have linked your javascript file to the html using:

<script src="nombre-de-tu-archivo.js"></script>

Or have written your js code as you did.

Scroll to Top