简体   繁体   中英

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;
}

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

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM