{"id":693,"date":"2011-08-18T23:31:26","date_gmt":"2011-08-19T05:31:26","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=693"},"modified":"2011-08-18T23:31:26","modified_gmt":"2011-08-19T05:31:26","slug":"code-of-the-day-check-to-see-if-a-element-has-a-event","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2011\/08\/code-of-the-day-check-to-see-if-a-element-has-a-event\/","title":{"rendered":"Code of the day: Check to see if a element has a event."},"content":{"rendered":"<pre lang='javascript'>\r\n\/\/ hasEvent checks to see if a element contains a event.\r\n\/\/ @requires jQuery 1.3.2+\r\n\/\/ @params el: string, jQuery object, node element.\r\n\/\/ @params eventName: string, name of the event.\r\n\/\/ @returns boolean\r\nvar hasEvent = function( el, eventName ){\r\n\tif( !$( el ).length || !$( el ).data( 'events' ) ){\r\n\t\treturn false;\r\n\t}\r\n\treturn !!$( el ).data( 'events' )[ eventName ];\r\n};\r\n<\/pre>\n<p><b>Demo:<\/b> (requires firebug or google chrome)<\/p>\n<pre lang='javascript'>\r\nvar el = $( '<div\/>' ).click( $.noop );\r\nconsole.clear();\r\nconsole.log( hasEvent( el, 'click' ) );\/\/ returns true;\r\n\r\nconsole.log( hasEvent( el, 'focusNow' ) );\/\/ returns false;\r\nel.bind( 'focusNow', $.noop );\r\nconsole.log( hasEvent( el, 'focusNow' ) );\/\/ returns true;\r\n\r\nconsole.log( hasEvent( document.body, 'onload' ) );\/\/ returns false;\r\n$(document.body).bind( 'onload', $.noop );\r\nconsole.log( hasEvent( document.body, 'onload' ) );\/\/ returns true;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ 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( &#8216;events&#8217; ) ){ return false; } &hellip; <a href=\"https:\/\/bateru.com\/news\/2011\/08\/code-of-the-day-check-to-see-if-a-element-has-a-event\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day: Check to see if a element has a event.<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,10],"tags":[30,87,164,5],"class_list":["post-693","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-tutorials","tag-code-of-the-day","tag-events","tag-javascript","tag-jquery"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=693"}],"version-history":[{"count":5,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":706,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/693\/revisions\/706"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}