Debug: How can I get the index of this element with jQuery?

Posted in Help the coder! on Mar 5, 2009 at 12:43 IST (about 1 year ago). Subscribe to this post Bookmark and Share Email
Showing comments 1 to 3 of total 3 on page 1 of 1
Post reply
« Previous1Next »

tuxsonna...
Rank: 266

Hi... I am trying to get the index of an element using jQuery to send to a PHP script. Here is my XHTML

<form action="/accreditation/questions/" method="post" id="questions-form">
<fieldset id="question-0">
<legend>Question</legend>
<h3>What colour is grass?</h3>
<ul>
<li><input type="radio" name="answer[0]" value="0" id="radio-0-0" /><label for="radio-0-0">Green</label></li>
<li><input type="radio" name="answer[0]" value="1" id="radio-0-1" /><label for="radio-0-1">Red</label></li>
<li><input type="radio" name="answer[0]" value="2" id="radio-0-2" /><label for="radio-0-2">Orange</label></li>
</ul>
</fieldset>
<fieldset id="question-1">
<legend>Question</legend>
<h3>how old is alex</h3>
<ul>
<li><input type="radio" name="answer[1]" value="0" id="radio-1-0" /><label for="radio-1-0">21</label></li>
<li><input type="radio" name="answer[1]" value="1" id="radio-1-1" /><label for="radio-1-1">11</label></li>
<li><input type="radio" name="answer[1]" value="2" id="radio-1-2" /><label for="radio-1-2">23</label></li>
</ul>
</fieldset>
</form>

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).

$('#questions-form ul li').each(function() {
qIndex = $('fieldset').index($('fieldset', $(this).parent()))
});

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).

I have played around for a while and have been unable to get the correct index. I keep getting not found (-1).

If it helps, here is the code I'm using to get the list item's index relative to it's containing ul element.

  index = $('li', $(this).parent()).index(this);

If anyone can show me what is wrong with my fieldset index grabbing code, that would be much appreciated!

Posted by tuxsonnaire on Thursday, March 5, 2009, 12:43 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

meenus
Rank: 11

LI's parent is UL, not the fieldset. I think this will set qIndex for you:

qIndex = $('fieldset').index($(this).parents('fieldset'));
Posted by meenus on Tuesday, March 10, 2009, 11:59 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

tweaknut
Rank: 105

$("form fieldset").each(function(I){ console.log("fieldset index"+ I"); });

Posted by tweaknut on Wednesday, March 11, 2009, 2:05 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment
Pages: « Previous1Next »

Post your comment (No registration required)

  Add my comment  

TechieDesi Community

Not signed in (Sign-in or Register)
Be a true TechieDesi!
Top 10 Users
Spread the word
Invite your friends
Fan stuff
Help us improve
Need Help
FAQ's
Search tips
Found a bug? Report!
Feeds and letters
Subscribe via RSS
Archives
Subscribe to newsletter
Unsubscribe e-mail
Miscellaneous
Privacy policy
Visit rootnerve
About us
About us
Support the development
Official Blog
Advertise with us
Careers
Copyright (c) 2008, TechieDesi.com. All rights reserved | About us | Do-Not-Disturb registry | Powered by rootnerve | Page rendered in 0.260 seconds