In below code, I pass an HMTL element and check whether parameter passed is null or not using ternary operator. If it is not null, I change the className of the passed element.
var changeColorTo = {
grey: function(e){
e ? (e.className = "grey") : "" ;
},
red: function(e){
e ? (e.className = "red") : "" ;
},
green: function(e){
e ? (e.className = "green") : "" ;
},
blue: function(e){
e ? (e.className = "blue") : "" ;
}
};
The above code works fine except when I pass any random string like
changeColorTo.grey("random");
It doesn't cause any harm. But I am wondering is above code correct? Do I miss anything? or is there any better way to achieve the same result?
Thank you.