Jeremy Vidaurri
Jeremy Vidaurri

Reputation: 49

How do I change an onclick function to have multiple functions?

I'm trying to change a button's onclick function to remove one function and add two functions. This is my current code

    var random;

    function number(){
        var random =Math.floor(Math.random()*Math.floor(Math.random()*20))

    }

function show(){
    var display = document.getElementById('number').innerHTML= random;
}

function start(){
    var random =Math.floor(Math.random()*Math.floor(Math.random()*20))
    var button = document.getElementById('button').innerHTML="I give up!!";
    var change = document.getElementById("button").onclick = show; number;
}

Upvotes: 1

Views: 237

Answers (2)

Sushanth --
Sushanth --

Reputation: 55750

Add 2 event listeners to your button

function start(){
    var random = Math.floor(Math.random()*Math.floor(Math.random()*20)),
        button = document.getElementById('button').innerHTML="I give up!!";
        changeButton = document.getElementById("button");

    changeButton.addEventListener('click', show);
    changeButton.addEventListener('click', number);
}

Upvotes: 1

dfsq
dfsq

Reputation: 193301

You need to assign onclick an anonymous function and include both functions in it:

document.getElementById("button").onclick = function() {
    show();
    number(); 
};

Upvotes: 1

Related Questions