0

Facing some problems with jQuery selectors.

My HTML is like:

<form method="" action=""> 
 <p id="question_1"> 
 <h1 id="question">1. A Question</h1> 
 <div id="choices"> 
 <p id="option1"><input type="radio" id="option" name="q_1" value="1" />A</p> 
 <p id="option2"><input type="radio" id="option" name="q_2" value="2" />B</p> 
 <p id="option3"><input type="radio" id="option" name="q_3" value="3" />C</p> 
 </div> 
 </p> 
. 
. 
. 
</form>

There are n such questions.

I want to highlight the <p> containing the 3rd radio button for each question. For example,

 p#question_1 > p#option3 
 p#question_2 > p#option3 
 p#question_3 > p#option3 

How to do that?

I was trying something like this, without any success:

$("form p#question_" + i).filter("p:eq(3)").addClass("correct");
user113716
323k64 gold badges454 silver badges441 bronze badges
asked Aug 4, 2010 at 18:22
4
  • 2
    It seems likely that you have duplicated IDs within your markup, which is invalid according to the specification. Commented Aug 4, 2010 at 18:26
  • Try using 'code sample' button in edit mode (the one with zeroes and ones). It's much easier than '&lt;' and '&gt;' Commented Aug 4, 2010 at 18:30
  • @karim79 - More than just likely, the radio elements all have the ID option. I agree that others (like choices) are likely as well. Commented Aug 4, 2010 at 18:30
  • IDs should be unique in an html document Commented Aug 4, 2010 at 18:31

5 Answers 5

1

I want to highlight the <p> containing the 3rd radio button for each question.

Use start with ^ selector with nth-child like this:

$('p[id^="question"]:nth-child(3)').addClass('correct');

What ^="question" does is that it selects all paragraphs starting with question text in their ids and then nth-child(3) is used to select the third paragraph.

More Info:

answered Aug 4, 2010 at 18:25
Sign up to request clarification or add additional context in comments.

Comments

1

If your third answer is always correct, you might opt for something like this instead:

<p class="first">Blah blah</p>
<p class="second">Blah blah</p>
<p class="third">Blah blah</p>

Then simply:

$("p.third").addClass("correct");

That should hopefully tempt you to get rid of those dupe IDs.

answered Aug 4, 2010 at 18:31

Comments

1

I assume you want this to work if you have many div's with answers in them? Decorate each such div with DivChoices, and then:

$(".DivChoices").each(function (index, item) { $("p", item).last().addClass('correct'); });
answered Aug 4, 2010 at 18:52

Comments

0

There are many ways of doing that, here is a nice jQuery selectors refcard that will be very useful for you in the future.

http://refcardz.dzone.com/refcardz/jquery-selectors

answered Aug 4, 2010 at 18:26

Comments

0

Would it be possible to just add a class to the third option for each question?

answered Aug 4, 2010 at 18:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.