js

Check if a jquery object has a particular class/id

Snippet

Use is() to check if a class exists on a jQuery element.

if ($(#elm).is('.checkthisclass')) { 
 // #elm has the class
} else { 
 //#elm doesn't have the class
}

PHP empty in javascript

Snippet

Equivalent of PHP's empty in javascript

function empty (mixed_var) {
 // version: 909.322
 // discuss at: http://phpjs.org/functions/empty
 var key;
 if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || mixed_var === undefined ) {
  return true;
 }
 if (typeof mixed_var == 'object') {
  for (key in mixed_var) {
   return false;
  }
  return true;
 }
 return false;
}

Parse JSON from server in javascript

Snippet

When a server response (say, from an ajax call) returns JSON formatted code, eval to bring the results into the script's space. Don't use with any returned JSON object that you don't really really really trust.

/*
 server returns 
 {"a":1,"b":2,"c":3,"d":4,"e":5}
 */
 
// NEVER DO THIS
var obj = eval('(' + jsontext + ')');
alert(obj.a);
// will receive "1" in alert (no quotes)
 
// DO THIS
JSON.parse( json.data );