How to run a Js function that is connected to a button more than once?

Asked

Viewed 28 times

0

Opa galera!

Next I have a function in the file "Script.js" which returns a message formatted with values obtained as follows::

Data manipulation:

let choice = ['a', 'b', 'c']
let resultado = []
resultado.push(choice[Math.floor((Math.random() * choice.length))])

Assignment of data output:

let div = document.getElementById("divId")

And finally the function that presents the manipulated data in a formatted way:

function Show() {
    let resultado = 'valor obtido = ' + resultado[0]
    div.InnerHTML = resultado
}

Until then ta show, I made a standard HTML document put the div and everything, I made a button and 'bindei' the function Show() in the onclick within the document Html, wonder worked but the button is clickable only once! Here comes my question, how do I press that the button again it presents a different result within the choices it has ('a', 'b', 'c')

  • 1

    Standard buttons can be clicked multiple times, if your button is working only the first time then there must be some condition and/or error that is preventing the normal functioning of it.

  • 3

    Are you sure the button only works once? Isn’t the fact that you’re pushing a value out of function, and calling the function to display in onClick? The way it’s there, every time you click it will display the same value, since you’re not changing it anywhere. I suggest putting this logic inside the function. Then every time you click will have a different random number

  • Perfect bro! I completely forgot this kkkk worked super well, thank you so much!

No answers

Browser other questions tagged

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