0

I am trying to select .singleAnswer but it's not working.

JQUERY:

 $(".answerContainer").on("click", ".editAnswer", function(e){
e.preventDefault();
answer = $(this).parent('.answerBar').closest('.singleAnswer'); //this variable doenst work

HTML:

<div class='answerContainer'>
<p name='singleAnswer' class='singleAnswer'>$answer[$f]</p>
<input type='hidden' value='$answerid[$f]' class='answer_id' />
<p class='ratingBox'> $answerrating[$f]</p>
<div class='answerBar'>
<a href='#' class='upVote vote'>Upvote</a> &middot; <a href='#' class='downVote vote'>Downvote</a>  &middot; 
<a class='answerTime'> $difference $periods[$j] ago</a>
 &middot; <a href='#' style='color: orange;' class='editAnswer'><b>Edit</b></a></div>
kirby
  • 3,981
  • 14
  • 41
  • 54
  • assuming `.answerContainer` is the parent of `singleAnswer`? – Loktar Jan 13 '12 at 02:18
  • @Loktar yes, edited original question – kirby Jan 13 '12 at 02:19
  • 1
    Three out of your last four questions are variations on not being able to traverse the tree. It would really help to read up on the [Document Object Model](https://developer.mozilla.org/en/DOM) and the [jQuery traversal methods](http://api.jquery.com/category/traversing/) – Dennis Jan 13 '12 at 02:51

2 Answers2

2

The .closest() method searches up through the DOM, where you seem to be looking for a sibling of the .answerBar element, or, really, a child of the .answerContainer element.

Try this:

answer = $(this).closest('.answerContainer').find('.singleAnswer'); 

This means go up through the DOM from this to the nearest .answerContainer element, then from there find descendent(s) with class .singleAnswer.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

You are looking for siblings not closest that would start the search up from the current element.

answer = $(this).parent('.answerBar').siblings('.singleAnswer');
amit_g
  • 30,880
  • 8
  • 61
  • 118