I want to write ternary conditional operator in jquery where condition set by jquery variable. My script toggle’s class for particular condition only.In my script variable comes from other settings.
jquery:
<script type="text/javascript">
$(document).ready(function() {
$("#my_div")
.removeClass("horizontal vertical")
.addClass( my_color == 'red' ? 'horizontal' : 'vertical');
});
</script>
my_color is jquery variable which may have one value at a time from red,green,blue,black,white or silver.
I want to set class horizontal if my_color is red, blue or green and class vertical for other three values. can anyone help me write simplified ternary operator for above jquery?
Use or operator in the condition part of the operator. You your would be
If you have many color you can put them array and use that in condition.