how to use java script code for two or more buttons

css, javascript

Solution

If you need to run the same function for different elements call the function name and not the function imploementation itself :

document.getElementById("wedding").onclick = lalala;

document.getElementById("divorce").onclick = lalala;

//inside you can use `this` ( notice we didnt pass [this] , it is done automatically)

function lalala()
{
     this.style.position = "absolute";
     this.style.left = '-450px';
     this.style.top = '-260px';
     this.style.zoom = 0.8;
     this.style.MozTransform = 'scale(0.8)';
     this.style.WebkitTransform = 'scale(0.8)';

}

After OP clarification

var e=document.getElementById("text") 

document.getElementById("wedding").onclick =function (){ lalala(e) };

document.getElementById("divorce").onclick =function (){ lalala(e) };




function lalala(elm)
{
     elm.style.position = "absolute";
     elm.style.left = '-450px';
     elm.style.top = '-260px';
     elm.style.zoom = 0.8;
     elm.style.MozTransform = 'scale(0.8)';
     elm.style.WebkitTransform = 'scale(0.8)';

}

Problem

``` var element = document.getElementById('text'); document.getElementById("wedding").onclick = function(){ element.style.position = "absolute"; element.style.left = '-450px'; element.style.top = '-260px'; element.style.zoom = 0.8; element.style.MozTransform = 'scale(0.8)'; element.style.WebkitTransform = 'scale(0.8)'; } ``` i need to use above code for two buttons. so i change this code like below, but its not working. how can i do that. ``` var element = document.getElementById('text'); document.getElementById("wedding divorce").onclick = function(){ element.style.position = "absolute"; element.style.left = '-450px'; element.style.top = '-260px'; element.style.zoom = 0.8; element.style.MozTransform = 'scale(0.8)'; element.style.WebkitTransform = 'scale(0.8)'; } ```

Original source