+ 6
How do I make it shorter?
I have several buttons (A, B and C), each one when pressed calls a different function, but they all do practically the same but with different value. How do I do this with only one function instead of three? Example: HTML: <button id="A">A</button> <button id="B">B</button> <button id="C">C</button> JavaScript: var Selected = "A"; function main(){ $("#A").on("click", A); $("#B").on("click", B); $("#C").on("click", C); } function A(){ if(Selected!="A") Selected = "A"; } function B(){ if(Selected!="B") Selected = "B"; } function C(){ if(Selected!="C") Selected = "C"; }
3 Antworten
+ 6
Or
function onSelect(ev){
var id= ev.currentTarget.id
if(Selected != id)
Selected= id
}
and assign to A, B, C same callback (onSelect)
+ 6
Thanks you KrOW!
+ 5
I've made a demo code
https://code.sololearn.com/WRvzQ9inSV9n/#html