Code of the day: Check to see if a element has a event.

// hasEvent checks to see if a element contains a event.
// @requires jQuery 1.3.2+
// @params el: string, jQuery object, node element.
// @params eventName: string, name of the event.
// @returns boolean
var hasEvent = function( el, eventName ){
	if( !$( el ).length || !$( el ).data( 'events' ) ){
		return false;
	}
	return !!$( el ).data( 'events' )[ eventName ];
};

Demo: (requires firebug or google chrome)

var el = $( '
' ).click( $.noop ); console.clear(); console.log( hasEvent( el, 'click' ) );// returns true; console.log( hasEvent( el, 'focusNow' ) );// returns false; el.bind( 'focusNow', $.noop ); console.log( hasEvent( el, 'focusNow' ) );// returns true; console.log( hasEvent( document.body, 'onload' ) );// returns false; $(document.body).bind( 'onload', $.noop ); console.log( hasEvent( document.body, 'onload' ) );// returns true;
Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

View Comments

  • Thanks for the great blog and comments that you blog contain are awesome. helped me a lot with my problem

Share
Published by
Larry Battle

Recent Posts

What really is Data Science? Told by a Data Scientist

What REALLY is Data Science? Told by a Data Scientist - By Joma Tech

7 years ago

Video: How Water Towers Work

How Water Towers Work - Practical Engineering

7 years ago

Dev Tip: Simple tips to improve code reviews

Writing perfect code is a challenging process. That's where code reviews come in to help…

7 years ago

Video: How AI will change the 3d industry

"The Next Leap: How A.I. will change the 3D industry - Andrew Price - Blender"

7 years ago

Best Software Presentation for 2018

"Captain Disillusion: World's Greatest Blenderer - Live at the Blender Conference 2018 - CaptainDisillusion"

7 years ago

Dev Video: A Few Linux Shell Tips

My 5 Favorite Linux Shell Tricks for SPEEEEEED (and efficiency) - By tutoriaLinux > What's…

7 years ago