Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As the two commenters noted you should have unique id's.</p> <p>I have a <a href="http://jsfiddle.net/Qtnnw/1/" rel="nofollow">jsFiddle</a>here for you on how to fetch id's.</p> <p>I added classes to the clickable elements and bound the click event on that. Assuming the h3 is on the same level as the 'inner' divs use the .prev function to find the h3 and fetch its id.</p> <p>That all being said the best practice is to really group your inner elements better and use unique id's. Multiple elements can have the same class but not ids.</p> <p>Slightly modified HTML:</p> <pre><code> &lt;div id="outer"&gt; &lt;h3 id="groupa"&gt;group a&lt;/h3&gt; &lt;div id="inner1" class="clickable"&gt;Click here&lt;/div&gt; &lt;div id="inner2" class="clickable"&gt;Click here&lt;/div&gt; &lt;h3 id="groupb"&gt;group b&lt;/h3&gt; &lt;div id="inner3" class="clickable"&gt;Click here&lt;/div&gt; &lt;div id="inner4" class="clickable"&gt;Click here&lt;/div&gt; &lt;/div&gt; &lt;br/&gt;&lt;br/&gt; Your Results: &lt;br/&gt;&lt;br/&gt; &lt;div class="output1"&gt;&lt;/div&gt;&lt;!--This inner's closest h3--&gt; &lt;div class="output2"&gt;&lt;/div&gt; &lt;!--This inner ID--&gt; </code></pre> <p>Javascript:</p> <pre><code>$('.clickable').click(function(){ //.attr fetches the attribute value $('.output1').text($(this).attr('id')); $('.output2').text($(this).prev('h3').attr('id')); /* $(this).attr('id') -- this contains the clicked element id $(this).prev('h3').attr('id') -- this contains the closest h3's id you could store them in a variable and then call your function with it. */ }); </code></pre>
    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.
    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