Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ($(selector).length > 0) {
- // Do something
- }
- if ($(selector).length)
- jQuery.fn.exists = function(){ return this.length > 0; }
- if ($(selector).exists()) {
- // Do something
- }
- jQuery.fn.exists = function(){return ($(this).length > 0);}
- if ($(selector).exists()) { }
- jQuery.exists = function(selector) {return ($(selector).length > 0);}
- if ($.exists(selector)) { }
- if ( $('#myDiv').length ) { /* Do something */ }
- if ( $('#myDiv')[0] ) { /* Do something */ }
- // if element exists
- if($('selector').length){ /* do something */ }
- // if element does not exist
- if(!$('selector').length){ /* do something */ }
- if (document.getElementById('element_id')) {
- // Do something
- }
- if ($(selector)[0]) { ... }
- if ($(selector).is('*')) {
- // Do something
- }
- ;;(function($) {
- if (!$.exist) {
- $.extend({
- exist: function() {
- var ele, cbmExist, cbmNotExist;
- if (arguments.length) {
- for (x in arguments) {
- switch (typeof arguments[x]) {
- case 'function':
- if (typeof cbmExist == "undefined") cbmExist = arguments[x];
- else cbmNotExist = arguments[x];
- break;
- case 'object':
- if (arguments[x] instanceof jQuery) ele = arguments[x];
- else {
- var obj = arguments[x];
- for (y in obj) {
- if (typeof obj[y] == 'function') {
- if (typeof cbmExist == "undefined") cbmExist = obj[y];
- else cbmNotExist = obj[y];
- }
- if (typeof obj[y] == 'object' && obj[y] instanceof jQuery) ele = obj[y];
- if (typeof obj[y] == 'string') ele = $(obj[y]);
- }
- }
- break;
- case 'string':
- ele = $(arguments[x]);
- break;
- }
- }
- }
- if (typeof cbmExist == 'function') {
- var exist = ele.length > 0 ? true : false;
- if (exist) {
- return ele.each(function(i) { cbmExist.apply(this, [exist, ele, i]); });
- }
- else if (typeof cbmNotExist == 'function') {
- cbmNotExist.apply(ele, [exist, ele]);
- return ele;
- }
- else {
- if (ele.length <= 1) return ele.length > 0 ? true : false;
- else return ele.length;
- }
- }
- else {
- if (ele.length <= 1) return ele.length > 0 ? true : false;
- else return ele.length;
- }
- return false;
- }
- });
- $.fn.extend({
- exist: function() {
- var args = [$(this)];
- if (arguments.length) for (x in arguments) args.push(arguments[x]);
- return $.exist.apply($, args);
- }
- });
- }
- })(jQuery);
- if ($.exist('#eleID')) { /* DO WORK */ } // param as STRING
- if ($.exist($('#eleID'))) { /* DO WORK */ } // param as jQuery OBJECT
- if ($('#eleID').exist()) { /* DO WORK */ } // enduced on jQuery OBJECT
- $.exist('#eleID', function() { // param is STRING && CALLBACK METHOD
- /* DO WORK */
- /* This will ONLY fire if the element EXIST */
- }, function() { // param is STRING && CALLBACK METHOD
- /* DO WORK */
- /* This will ONLY fire if the element DOES NOT EXIST */
- })
- $('#eleID').exist(function() { // enduced on jQuery OBJECT with CALLBACK METHOD
- /* DO WORK */
- /* This will ONLY fire if the element EXIST */
- })
- $.exist({ // param is OBJECT containing 2 key|value pairs: element = STRING, callback = METHOD
- element: '#eleID',
- callback: function() {
- /* DO WORK */
- /* This will ONLY fire if the element EXIST */
- }
- })
- $.fn.exists = $.fn.exists || function() {
- return !!(this.length && (this[0] instanceof HTMLDocument || this[0] instanceof HTMLElement));
- }
- $(1980).exists(); //return false
- $([1,2,3]).exists(); //return false
- $({name: 'stackoverflow', url: 'http://www.stackoverflow.com'}).exists(); //return false
- $([{nodeName: 'foo'}]).exists() // returns false
- $('div').exists(); //return true
- $('.header').exists(); //return true
- $(document).exists(); //return true
- $('body').exists(); //return true
- if(document.getElementById("myElement")) {
- //Do something...
- }
- jQuery.fn.extend({
- exists: function() { return this.length }
- });
- if($(selector).exists()){/*do something*/}
- function elementIfExists(selector){ //named this way on purpose, see below
- return document.querySelector(selector);
- }
- /* usage: */
- var myelement = elementIfExists("#myid") || myfallbackelement;
- var myel=elementIfExists("#myid");
- // now we are using a reference to the element which will linger after removal
- myel.getParentNode.removeChild(myel);
- console.log(elementIfExists("#myid")); /* null */
- console.log(myel); /* giant table lingering around detached from document */
- myel=null; /* now it can be garbage collected */
- /* locally scoped myel gets garbage collected even with the break; */
- for (var myel; myel = elementIfExist(sel); myel.getParentNode.removeChild(myel))
- if (myel == myblacklistedel) break;
- function elementExists(selector){
- return !!document.querySelector(selector);
- }
- /* usage: */
- var hastables = elementExists("table"); /* will be true or false */
- if (hastables){
- /* insert css style sheet for our pretty tables */
- }
- setTimeOut(function (){if (hastables && !elementExists("#mytablecss"))
- alert("bad table layouts");},3000);
- var $target = $({});
- console.log($target, $target.length);
- // Console output:
- // -------------------------------------
- // [▼ Object ] 1
- // ► __proto__: Object
- if ($.isEmptyObject(selector) || !$(selector).length) {
- throw new Error('Unable to work with the given selector.');
- }
- $.isEmptyObject('hello') // FALSE in Chrome and TRUE in IE
- // These by Id
- if( $('#elementid').length > 0){
- // Element is Present
- }else{
- // Element is not Present
- }
- // These by Class
- if( $('.elementClass').length > 0){
- // Element is Present
- }else{
- // Element is not Present
- }
- $(selector).length && //Do something
- if ($("#myDiv").length) {
- $("#myDiv").show();
- }
- $("#myDiv").show();
- jQuery.fn.exists = function(){return !!this.length};
- if ($(selector).exists()) {
- // the element exists, now what?...
- }
- if (!!$(selector)[0]) // do stuff
- $.fn.exists = function() {
- return $.contains( document.documentElement, this[0] );
- }
- function isExists(selector){
- return document.querySelectorAll(selector).length>0;
- }
- // Checks if an object exists.
- // Usage:
- //
- // $(selector).exists()
- //
- // Or:
- //
- // $(selector).exists(anotherSelector);
- jQuery.fn.exists = function(selector) {
- return selector ? this.find(selector).length : this.length;
- };
- $.fn.exists = function(callback) {
- var self = this;
- var wrapper = (function(){
- function notExists () {}
- notExists.prototype.otherwise = function(fallback){
- if (!self.length) {
- fallback.call();
- }
- };
- return new notExists;
- })();
- if(self.length) {
- callback.call();
- }
- return wrapper;
- }
- $("#elem").exists(function(){
- alert ("it exists");
- }).otherwise(function(){
- alert ("it doesn't exist");
- });
- $.fn.exists = (callback) ->
- exists = @length
- callback.call() if exists
- new class
- otherwise: (fallback) ->
- fallback.call() if not exists
- if(document.querySelector('.a-class')) {
- // do something
- }
- function exists(selector) {
- return $(selector).length;
- }
- if (exists(selector)) {
- // do something
- }
- $.fn.ifExists = function(fn) {
- if (this.length) {
- $(fn(this));
- }
- };
- $("#element").ifExists(
- function($this){
- $this.addClass('someClass').animate({marginTop:20},function(){alert('ok')});
- }
- );
- $.fn.exist = function(callback) {
- return $(this).each(function () {
- var target = $(this);
- if (this.length > 0 && typeof callback === 'function') {
- callback.call(target);
- }
- });
- };
- $.fn.exist = function(onExist, onNotExist) {
- return $(this).each(function() {
- var target = $(this);
- if (this.length > 0) {
- if (typeof onExist === 'function') {
- onExist.call(target);
- }
- } else {
- if (typeof onNotExist === 'function') {
- onNotExist.call(target);
- }
- }
- });
- };
- $('#foo .bar').exist(
- function () {
- // Stuff when '#foo .bar' exists
- },
- function () {
- // Stuff when '#foo .bar' does not exist
- }
- );
- if($("selector").length){
- //code in the case
- }
- if ( $('#myDiv').size() > 0 ) { //do something }
- if($(selector).length){
- // true if length is not 0
- } else {
- // false if length is 0
- }
Add Comment
Please, Sign In to add comment