Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get the index of this element with jQuery?
    primarykey
    data
    text
    <p>I am trying to get the index of an element using jQuery to send to a PHP script.</p> <p><strong>Here is my XHTML</strong></p> <pre><code>&lt;form action="/accreditation/questions/" method="post" id="questions-form"&gt; &lt;fieldset id="question-0"&gt; &lt;legend&gt;Question&lt;/legend&gt; &lt;h3&gt;What colour is grass?&lt;/h3&gt; &lt;ul&gt; &lt;li&gt; &lt;input type="radio" name="answer[0]" value="0" id="radio-0-0" /&gt; &lt;label for="radio-0-0"&gt;Green&lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="radio" name="answer[0]" value="1" id="radio-0-1" /&gt; &lt;label for="radio-0-1"&gt;Red&lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="radio" name="answer[0]" value="2" id="radio-0-2" /&gt; &lt;label for="radio-0-2"&gt;Orange&lt;/label&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/fieldset&gt; &lt;fieldset id="question-1"&gt; &lt;legend&gt;Question&lt;/legend&gt; &lt;h3&gt;how old is alex&lt;/h3&gt; &lt;ul&gt; &lt;li&gt; &lt;input type="radio" name="answer[1]" value="0" id="radio-1-0" /&gt; &lt;label for="radio-1-0"&gt;21&lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="radio" name="answer[1]" value="1" id="radio-1-1" /&gt; &lt;label for="radio-1-1"&gt;11&lt;/label&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="radio" name="answer[1]" value="2" id="radio-1-2" /&gt; &lt;label for="radio-1-2"&gt;23&lt;/label&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>I need to get the index of the fieldset elements. I am currently using this each iterator (which I'd like not to change because it has a lot of other functions inside it).</p> <pre class="lang-js prettyprint-override"><code>$('#questions-form ul li').each(function() { qIndex = $('fieldset').index($('fieldset', $(this).parent())) }); </code></pre> <p>I'd like qIndex to be the index of fieldset relative to the form. For example, in this case I should have it equal to 0 and 1 (although there would be 6 values because it's looping through the list elements).</p> <p>I have played around for a while and have been unable to get the correct index. I keep getting not found (-1).</p> <p>If it helps, here is the code I'm using to get the list item's index relative to it's containing ul element.</p> <pre class="lang-js prettyprint-override"><code>index = $('li', $(this).parent()).index(this); </code></pre> <h3>What is wrong with my fieldset index grabbing code?</h3>
    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.
 

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