0

Creating A Button Element to perform dual function , first click to first function , 2nd click for Second Function,

Create a button Which brings the change in element like <p id="p">This is paragraph </p>. // Css color of this is black <button onclick=change()></button>//html part // JS part Function change(){ Var ch = Document.getElementById("p"); ch.style.color="red"; } Then after the change comes in effect , the button itself chnge its functon to new functon linke Undo function Function undo( ){ ch.style.color="black"; } Suggest a way for performing both function alternatively by one button element

8th Jan 2019, 7:37 AM
ASHISH PANDEY
ASHISH PANDEY - avatar
3 Answers
+ 3
Do smt this: //HTML <p id="p"></p> <button onclick=click()></button> //JS var state = "black" function click(){ var ch = document.getElementById("p") if(state==="black"){ state = "red"; ch.style.color="red"; } else { state = "black" ch.style.color="black"; } }
8th Jan 2019, 7:55 AM
Maneren
Maneren - avatar
+ 2
Further to Maneren 's answer, if you want to avoid messing up with the flag state, use closure.
8th Jan 2019, 12:32 PM
Gordon
Gordon - avatar
+ 1
Just call one function with an integer parameter and write any actions to that function, so you have <button ... onclick="f(n)">. This way you dont need more than one function for this task.
8th Jan 2019, 10:03 AM
Bebida Roja
Bebida Roja - avatar