Search
 
SCRIPT & CODE EXAMPLE
 

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'}); 
////////////////////////////////////
Comment

PREVIOUS NEXT
Code Example
Javascript :: js random generator 
Javascript :: Find duplicate or repeat elements in js array 
Javascript :: angular get name of component 
Javascript :: object copy in javascript 
Javascript :: auto scroll to view react-native 
Javascript :: svg path clickable 
Javascript :: clear input field data in jquery 
Javascript :: learn mongodb 
Javascript :: mongoose user model example 
Javascript :: node.js check if a remote URL exists 
Javascript :: Get the current tab 
Javascript :: last item in array javascript 
Javascript :: how to detect if an video is over js html 
Javascript :: js how to calculate factorial 
Javascript :: puppeteer event element change 
Javascript :: vim go back word 
Javascript :: removeitem localstorage 
Javascript :: pop up notification using jquery 
Javascript :: arrow functions 
Javascript :: json arrays 
Javascript :: Get Keys Of JSON As Array 
Javascript :: using arrow function and destructuring 
Javascript :: for of javascript 
Javascript :: node package.json type module 
Javascript :: how to check what browser you are using 
Javascript :: trailing comma javascript 
Javascript :: combine the values of 2 arrays in key = value jquery 
Javascript :: javascript how to merge arrays 
Javascript :: get id in url params 
Javascript :: get value of textarea jquery 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =