{"id":228,"date":"2022-08-30T14:57:51","date_gmt":"2022-08-30T14:57:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/08\/31\/best-way-to-check-javascript-objects-value-if-object-may-be-undefined-record-and-share-programming-errors\/"},"modified":"2022-08-30T14:57:51","modified_gmt":"2022-08-30T14:57:51","slug":"best-way-to-check-javascript-objects-value-if-object-may-be-undefined-record-and-share-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/best-way-to-check-javascript-objects-value-if-object-may-be-undefined-record-and-share-programming-errors\/","title":{"rendered":"Best way to check JavaScript object&#39;s value if object may be undefined-Record and share programming errors"},"content":{"rendered":"<p>I have a <code>textarea<\/code> that I have a blur event attached to. I don&#8217;t want the blur event to run if the user clicks on a specific <code>span.dontClick<\/code> on the page. However if the user clicks on anything other than the <code>span.dontClick<\/code> I do want the blur event to run. I have found a way to do this, but it looks awful, feels very hackey, and I&#8217;m guessing there is a better solution out there. Here&#8217;s what I&#8217;ve come up with:<\/p>\n<pre><code>if (event.type === 'focusout') {\n  if (\n    typeof event.originalEvent !== 'undefined' &amp;&amp;\n    typeof event.originalEvent.currentTarget !== 'undefined' &amp;&amp;\n    typeof event.originalEvent.currentTarget.activeElement !== 'undefined' &amp;&amp;\n    typeof event.originalEvent.currentTarget.activeElement.className !== 'undefined' &amp;&amp;\n    event.originalEvent.currentTarget.activeElement.className === 'dontClick'\n  ) {\n    \/\/ abort the code for the blur event (in effect, do nothing)\n  }\n  else {\n    \/\/ run code for when a user blurs by not clicking on span\n  }\n<\/code><\/pre>\n<p>It should probably be noted that the <code>event<\/code> object is from a jQuery event handler. I&#8217;m not sure if it is different or the same as the native <code>event<\/code> objects.<\/p>\n<p>It should also be noted that this fails silently for most browsers but I get a debug window in IE if I don&#8217;t specify the &#8216;undefined&#8217; state for each object all the way down the line&#8230;<\/p>\n<p>If anyone has a more elegant solution I would really appreciate seeing it. Thanks!<\/p>\n<ol>\n<li>\n<p>You don&#8217;t need comparisons to <code>undefined<\/code>. Just do a truthy test. Also the test for the <code>className<\/code> isn&#8217;t needed. The <code>===<\/code> comparison will cover it.<\/p>\n<pre><code> if (\n    event.originalEvent &amp;&amp;\n    event.originalEvent.currentTarget &amp;&amp;\n    event.originalEvent.currentTarget.activeElement &amp;&amp;\n    event.originalEvent.currentTarget.activeElement.className === 'dontClick'\n  ) {\n<\/code><\/pre>\n<p>but then you&#8217;re doing more work than needed anyway since jQuery gives you a reliable <code>currentTarget<\/code>.<\/p>\n<pre><code> if (\n    event.currentTarget.activeElement &amp;&amp;\n    event.currentTarget.activeElement.className === 'dontClick'\n  ) {\n<\/code><\/pre>\n<p>or if you&#8217;re in a handler, just use <code>this<\/code>.<\/p>\n<pre><code> if (this.activeElement &amp;&amp; this.activeElement.className === 'dontClick') {\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-08-31 05:11:48. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a textarea that I have a blur event attached to. I don&#8217;t want the blur event to run if the user clicks on a specific span.dontClick on the page. However if the user clicks on anything other than the span.dontClick I do want the blur event to run. I have found a way [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}