Categories
JavaScript jQuery Web

JQuery Check if Element Exists – JS Helpers

JQuery Check if Element Exists using this simple piece of code. To check this in the current page, we need to check the length of the element returned by the JQuery selector, if it returns you something then the element must exists otherwise no.

if( $('#EleSelector').length ) { // use this if you are using id, replace EleSelector with the id
// it exists
}

if( $(‘.EleSelector’).length ) { // use this if you are using class, replace EleSelector with the id
// it exists
}

In case of plain javascript try this:

//you can use it for more advanced selectors
if(document.querySelectorAll("#EleSelector").length){}

if(document.querySelector("#EleSelector")){}

//you can use it if your selector has only an Id attribute
if(document.getElementById("EleSelector")){}

Categories
JavaScript jQuery Web

JQuery Check if Checkbox is checked – Simple Solution

For a check box with the id “checkbox”, here is how to check if its checked:

$('#checkbox').is(':checked'); // Returns True if checked else False

Categories
JavaScript Web

JavaScript Remove duplicates from Array – Example Code

Here is the code to remove duplicates from an Array:

var distinctVal = function(arr){
var newArray = [];
for(var i=0, j=arr.length; i<j; i++){
if(newArray.indexOf(arr[i]) == -1)
newArray.push(arr[i]);
}
return newArray;
};

Categories
JavaScript Web

JavaScript Basics: How to check for null & undefined

To check if a variable(a) has the value null, use the following code:

if (a === null) // Best way to do it!

if (a == null) // This does return true if a is null but it returns true also if a is undefined... so avoid it

To check if a variable(a) has the value undefined, use the following code:

if (typeof a === "undefined") // This works

if (a === undefined) // This works too

if (a == undefined) // This does return true if a is undefined but it returns true also if a is null... so avoid it

Categories
JavaScript jQuery Web

JavaScript Basics: Difference between == & === (with examples)

JavaScript has two sets of equality operators: === and ==. They mostly work exactly the same ie, if the operands are of the same type and have the same value, then === produces true & so does ==. But if the operands are of different types, then === produces false & == says true. The simplest reason is that == is also a type converter.

Here are some examples of === & == in action:

true == 1 // true
true === 1 // false, because they are of a different type
1 == "1" // true, automatic type conversion for value only
1 === "1" // false, because they are of a different type
null == undefined // true
null === undefined // false

== is also notoriously inconsistent. See the below examples:

'' == '0' // false
0 == '' // true
0 == '0' // true

false == 'false' // false
false == '0' // true

false == undefined // false
false == null // false
null == undefined // true

' \t\r\n ' == 0 // true

Best practice is to avoid == and use === consistently in your code.