Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To get the id of the parent div:</p> <pre><code>$(buttonSelector).parents('div:eq(0)').attr('id'); </code></pre> <p>Also, you can refactor your code quite a bit:</p> <pre><code>$('button').click( function() { var correct = Number($(this).attr('rel')); validate(Number($(this).siblings('input').val()), correct); $(this).parents('div:eq(0)').html(feedback); }); </code></pre> <p>Now there is no need for a button-class</p> <p><b>explanation</b><br> eq(0), means that you will select one element from the jQuery object, in this case element 0, thus the first element. <a href="http://docs.jquery.com/Selectors/eq#index" rel="noreferrer">http://docs.jquery.com/Selectors/eq#index</a><br> $(selector).siblings(siblingsSelector) will select all siblings (elements with the same parent) that match the siblingsSelector <a href="http://docs.jquery.com/Traversing/siblings#expr" rel="noreferrer">http://docs.jquery.com/Traversing/siblings#expr</a><br> $(selector).parents(parentsSelector) will select all parents of the elements matched by selector that match the parent selector. <a href="http://docs.jquery.com/Traversing/parents#expr" rel="noreferrer">http://docs.jquery.com/Traversing/parents#expr</a><br> Thus: $(selector).parents('div:eq(0)'); will match the the first parent div of the elements matched by selector.</p> <p>You should have a look at the jQuery docs, particularly selectors and traversing: </p> <ul> <li><a href="http://docs.jquery.com/" rel="noreferrer">http://docs.jquery.com/</a></li> <li><a href="http://docs.jquery.com/Selectors" rel="noreferrer">http://docs.jquery.com/Selectors</a></li> <li><a href="http://docs.jquery.com/Traversing" rel="noreferrer">http://docs.jquery.com/Traversing</a></li> </ul>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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