I made a button in HTML and when I clicked it the first action worked but the second one didn’t. I want the same button to replace the text with new text after the second click.
<body>
<button onclick=dosomething(); onclick=dosomething2() style="background-color:
aquamarine;color:purple;
font-size:40px;border-radius:100px;">Click Me!</button>
<p style="color:red;font-size:80px;" id="test"></p>
<script>
function dosomething(){
document.getElementById("test").innerHTML = "Why did you do what it said >:("
}
</script>
<script>
function dosomething2(){
document.getElementById("test").innerHTML = "Stop it >:("
}
</script>
</body>
3
Answers
You can run 2 actions from one button by creating 1 function with all functions you need to run in it. You can modify your code like this:
Only the first instance of the onclick attribute will be treated. If you know function 2 will always follow function 1, you could have them chain each other.
try
Change the onclick of the button right after using dosomething.