{"id":93,"date":"2011-07-01T10:18:16","date_gmt":"2011-07-01T17:18:16","guid":{"rendered":"http:\/\/www.designersgate.com\/blogs\/?p=93"},"modified":"2011-07-01T10:18:16","modified_gmt":"2011-07-01T17:18:16","slug":"getting-the-id-of-an-element-with-plain-javascript","status":"publish","type":"post","link":"https:\/\/designersgate.com\/blog\/getting-the-id-of-an-element-with-plain-javascript\/","title":{"rendered":"Getting the ID of an element with plain JavaScript"},"content":{"rendered":"<p>I was searching for this for very long and didn&#8217;t really found something useful unless using a\u00a0JavaScript\u00a0framework, but for those who want to learn to code from scratch this is the answer.<\/p>\n<p>First you have to get the element that triggers an event. example:<\/p>\n<p><code>HTML<\/code><\/p>\n<pre><span style=\"color: #0000ff;\">&lt;div id=\"hello\" onclick=\"getId();\"&gt;&lt;\/div&gt;<\/span>\n<span style=\"color: #888888;\">&lt;!-- in Firefox is necessary to include the event in the function --&gt;<\/span>\n<span style=\"color: #0000ff;\">&lt;div id=\"hello\" onclick=\"getId(event);\"&gt;&lt;\/div&gt;<\/span><\/pre>\n<p>In the HTML page header add the <code>getId(e)<\/code> function with: (note that I didn&#8217;t pass any value in the html code for any browser except <span style=\"color: #ff9900;\">Firefox<\/span>)<\/p>\n<pre><span style=\"color: #0000ff;\">function <span style=\"color: #008000;\">getId<\/span>(e){<\/span>\n<span style=\"color: #0000ff;\"> var <span style=\"color: #008000;\">tgt<\/span>;<\/span>\n<span style=\"color: #0000ff;\"> var <span style=\"color: #008000;\">id<\/span>;<\/span>\n<span style=\"color: #0000ff;\"> if (!e) var <span style=\"color: #008000;\">e<\/span> = <span style=\"color: #008000;\">window<\/span>.<span style=\"color: #ff00ff;\">event<\/span>; <span style=\"color: #808080;\">\/\/ if e is undefined<\/span><\/span>\n<span style=\"color: #0000ff;\"> if (e.<span style=\"color: #ff00ff;\">target<\/span>) <span style=\"color: #008000;\">tgt<\/span> = e.<span style=\"color: #ff00ff;\">target<\/span>; <span style=\"color: #808080;\">\/\/ For most browsers <\/span><\/span>\n<span style=\"color: #0000ff;\"> else if (e.<span style=\"color: #ff00ff;\">srcElement<\/span>) tgt = e.<span style=\"color: #ff00ff;\">srcElement<\/span>;<span style=\"color: #808080;\"> \/\/ For microsoft browsers <\/span><\/span>\n<span style=\"color: #0000ff;\"> if (<span style=\"color: #008000;\">tgt<\/span>.<span style=\"color: #ff00ff;\">nodeType<\/span> == <span style=\"color: #ff0000;\">3<\/span>) <span style=\"color: #808080;\">\/\/ To avoid webkit bug <\/span><\/span>\n<span style=\"color: #0000ff;\"><span style=\"color: #008000;\"> tgt<\/span> = <span style=\"color: #008000;\">tgt<\/span>.<span style=\"color: #ff00ff;\">parentNode<\/span>; <\/span>\n<span style=\"color: #0000ff;\"><span style=\"color: #008000;\"> id<\/span> = <span style=\"color: #008000;\">tgt<\/span>.<span style=\"color: #008000;\">getAttribute<\/span>(<span style=\"color: #ff0000;\">'id'<\/span>,<span style=\"color: #ff0000;\">2<\/span>);<\/span>\n<span style=\"color: #0000ff;\"><span style=\"color: #ff00ff;\"> alert<\/span>(<span style=\"color: #008000;\">id<\/span>);<\/span>\n<span style=\"color: #0000ff;\">}<\/span><\/pre>\n<p>Every DOM event carry out the event information, you only has to targeted, and of course all mayor browser do it differently so you need to code so the right event is processed. Most browsers uses <span style=\"color: #339966;\"><code>window.event.target<\/code><\/span> while Microsoft uses <span style=\"color: #339966;\"><code>window.event.srcElement<\/code><\/span>.<\/p>\n<p>First the function try to locate the variable e value, because we didn&#8217;t provide it in the html <code>e<\/code> variable is undefined, so the function create the event, then try to assign the <span style=\"color: #339966;\"><code>window.event.target<\/code><\/span> as the variable <span style=\"color: #0000ff;\"><code>tgt<\/code><\/span> value, if this doesn&#8217;t work, the creates the <span style=\"color: #339966;\"><code>window.event.srcElement<\/code><\/span> object as the variable value. If this failed, for Safari bug then it looks for the <code>window.event.nodeType<\/code> object and assigned it to the variable.<\/p>\n<p>Once we have our object assign to the variable the function call for the <span style=\"color: #0000ff;\"><code>getAttribute()<\/code><\/span> element object. First you tell the attribute name you are targeting, in this case the <code>id<\/code> attribute. The second value 2 is to get exactly the value of the attribute. Another options are 0 that&#8217;s a default and perform a non-case sensitive search or 1 that perform a case-sensitive search of the value. I decided to use 2 because returns the property value as it is set in the script or html code.<\/p>\n<p>That&#8217;s it. Nothing to it, I hope this help my fellow coders.<\/p>\n<p>Thanks to<br \/>\n<a href=\"http:\/\/www.quirksmode.org\/js\/events_properties.html\" target=\"_blank\" rel=\"noopener noreferrer\"> www.quirckmodes.com<\/a><br \/>\n<a href=\"http:\/\/www.roseindia.net\/javascript\/javascript-getattribute-method.shtml\" target=\"_blank\" rel=\"noopener noreferrer\"> www.roseindia.net<\/a><\/p>\n<p>Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was searching for this for very long and didn&#8217;t really found something useful unless using a\u00a0JavaScript\u00a0framework, but for those who want to learn to code from scratch this is the answer. First you have to get the element that triggers an event. example: HTML &lt;div id=&#8221;hello&#8221; onclick=&#8221;getId();&#8221;&gt;&lt;\/div&gt; &lt;!&#8211; in Firefox is necessary to include [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[70,71,72,16,73],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-dom-elements","tag-element","tag-id","tag-javascript","tag-jquery"],"acf":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":0,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"wp:attachment":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}