I have a click event that calls a function (below, txtResize) which makes some CSS changes to html text. (Acts on Text of 3 unique elements).
Q: What is the best way to refactor this code to limit excessive if/else usage, AND restore the original state of ALL text elements once the user clicks away from this section?
Here is the partially working, ugly function:
function txtResize() {
var clicked = this.id;
if (clicked == "Text1")
{
$("#" + clicked).css("opacity", "1");
$("#Text2, #Text3").css("opacity", "0.2");
}
else if (clicked == "Text2")
{
$("#" + clicked).css("opacity", "1");
$("#Text1, #Text3").css("opacity", "0.2");
}
else if (clicked == "Text3")
{
$("#" + clicked).css("opacity", "1");
$("#Text1, #Text2").css("opacity", "0.2");
}
// code above is ok, below does not work
else if ( !(clicked == "Text1") && !(clicked == "Text2") && !(clicked == "Text3") )
{
$("#Text1, #Text2, #Text3").css("opacity", "1"); // return to default state
}
};