lbreau
lbreau

Reputation: 63

passing index value to use as a selector in jquery

REVISED EDIT as of 4/28. I must have some selection error.

An updated jsFiddle is here: http://jsfiddle.net/thumbslinger/UwFRm/44/

What should happen, is that when one clicks on a ".correct" answer, the corresponding "rightAnwer" should become visible. This is the snippet from the fiddle that isn't working:

$('.correct').click(function(){
     var findMe = $(this).parent().parent().index();
    $('.answer').eq(findMe).show();
     });

The correct "answer" is being shown, however, I just want the div with the class of '.rightAnswer', which is a child of 'answer' to show.

//////////// ORIGINAL POST BELOW //////////////////////

killing me. NOTE: I asked something similar but the issue now is passing a value versus getting the value. I'm wondering if using eq() is an issue w/show/hide.

This returns the value I need:

var findMe = $(this).parent().parent().index();

Now I am trying to use that value as part of a selection for another element:

$('.correct').click(function(){
   var findMe = $(this).parent().parent().index();

   $('#fullSongs_Container .answer:eq(findMe)').show();
     }); 

and I've tried: $('#fullSongs_Container .answer').eq(findMe).show();

So, I'm wanting to use the same index value of the first line as a filter when choosing another div in a different container.

Upvotes: 1

Views: 1254

Answers (2)

lbreau
lbreau

Reputation: 63

Figured it out for future reference. My selector was off. In the nested pattern of:

<div class="question">
  <div class="rightAnswer"></div>
  <div class="wrongAnswer"></div>
</div>


$('.answer .rightAnswer').eq(findMe).show();

is what I needed.

Though jquery reads from right to left in terms of selection, this threw me off because I considered the eq() and show() to be part of the right-to-left actions to take rather than thinking just of the selections then the actions to take.

Everyone here probably knows that but now I see.

Upvotes: 0

KBN
KBN

Reputation: 2984

Change your code

$('#fullSongs_Container .answer:eq(findMe)').show();

TO

$('#fullSongs_Container .answer:eq(' + findMe + ')').show();

If you did not understand what I did. In your existing code, "findMe" is just a string and not a variable. In the modified code, findMe is treated as a variable and it's value will be substituted there.

Upvotes: 4

Related Questions