Function to add to the click but with values within the parentheses

0
<button onclick="funcion6();">
    Dame click!
</button>
<p id="demo6">Yo cambiare al darle click</p>


<script>
function funcion6(a,b) {
    return a + b;       
}
document.getElementById('demo6').innerHTML = funcion6(2,3);
</script>

This already worked for me.

<button onclick="document.getElementById('demo5').innerHTML = funcion5(8,9);">Click para hacer la pinshi suma</button> 

But I'm looking for a way to do it without putting so much code in the on-click method. I hope you can help me, thank you.

    
asked by user23028 01.12.2017 в 01:33
source

1 answer

1

Try defining what encloses all the logic and thus only define the function.

For example:

function funcion6(){
  document.getElementById("demo6").innerHTML = "Cambie!";
}

function funcion5(a,b) {
    return a + b;       
}

function Sumar(){
  document.getElementById('demo5').innerHTML = funcion5(8,9);
}
<button onclick="funcion6();">
    Dame click!
</button>
<p id="demo6">Yo cambiare al darle click</p>
<p id="demo5"></p>
<button onclick="Sumar()">Click para hacer la pinshi suma</button>

The Sumar() function does everything you had in the onclick of the button.

    
answered by 01.12.2017 / 02:57
source