Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

ternary operator jquery

// Ternary Operators in Jquery Work the same as in Normal JS 
condition ? exprIfTrue : exprIfFalse
// tell me IF 'a' is bigger or smaller than zero
	a>0 ?  $("#display").text(a + " is greater than 0"):  $("#display").text(a + " is less than or equal 0");
//if true // then do this								  ELSE do this 
/////////////////////////////////////////////////////////////////////
//EXAMPLE:
// lets say we have a box with an ID of blackbox 
// we are trying to change its background color (using Jquery)
// on the following condition: if the box is pink then it turns black,
// but if it's been turned black, then it to go back to pink.
/////////////////////////////////////////////////////////////////////
var box = $("#blackbox");
// If this condtion is true 	 // then apply this background		//Else Apply this background
box.css('background') == 'pink' ? box.css({'background':'black'}) : box.css({'background':'pink'}); 
////////////////////////////////////
 
PREVIOUS NEXT
Tagged: #ternary #operator #jquery
ADD COMMENT
Topic
Name
7+3 =