Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate ID dynamically based on position in DOM
    primarykey
    data
    text
    <p>I need to generate a value of element based on the level in the DOM. e.g. If I have a click event at document level (i.e. listening to any clicks on the pages) and the rendered HTML like this, it needs to traverse through DOM until you see "ID" attribute populated.</p> <pre><code>&lt;div id="someid"&gt; &lt;div&gt; &lt;span&gt; &lt;p&gt; &lt;a href="javascript:void(0);"&gt; &lt;span&gt; &lt;strong&gt;Googlie&lt;/strong&gt; &lt;/span&gt; &lt;/a&gt; &lt;/p&gt; &lt;/span&gt; &lt;/div&gt; </code></pre> <p></p> <p>When I click on "Googlie", it should generate something like this: someid__div_span_p_a_span_strong</p> <p>Issue: I do have some code to do this but there is a problem with it. In above, what if I have two elements, right after another as shown below. How do I generate an unique value for strong element? Can I have something without using childNodes location? The childNodes value changes if someone changes the white space between the elements. </p> <pre><code>...&lt;span&gt; &lt;strong&gt;Googlie&lt;/strong&gt; &lt;br/&gt; &lt;strong&gt;Bingie&lt;/strong&gt; </code></pre> <p>...</p> <pre><code> document.onclick = function(obj) { var node = obj.target; var res = []; var etA = ""; res[0] = false; while (!res[0]) { res = GetIdFor(node); if (!res[0]) { var end = node.parentNode.childNodes.length; for (var i = 0; i &lt; end; i++) { var j = node.parentNode.childNodes[i]; if (j.nodeType === 1 &amp;&amp; j === node) { break; } } etA = "_" + node.tagName + etA; node = node.parentNode; } else { etA = res[1] + "_" + etA; } } alert(etA); }; function GetIdFor(elem) { var res = []; res[0] = false; var d = elem.getAttribute("ID"); if (d !== "undefined" &amp;&amp; d !== null) { res[0] = true; res[1] = d; } return res; } </code></pre> <p>Now, I know jQuery can help here but that's not THE point. :) Thoughts? Do I not have any other choice but using childNodes level in order to generate this value?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload