Jquery tree traversing not selecting

79 views Asked by At

I have some jquery variables that attempt to get values of nearby elements however they don't work. I thought closest() was the right selector.

JQUERY:

$(".answerContainer").on("click", ".editAnswer", function(e){
e.preventDefault();

var answer_id = $(this).closest('.answer_id').attr('value'); //this doenst work

HTML:

<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>
3

There are 3 answers

0
Gabe On BEST ANSWER

Try this, also use .val() instead of attr('value')

   var answer_id = $('.editAnswer').parent().siblings('.answer_id').val();
0
Cefn Hoile On

Closest assumes you want to traverse through the ancestors of the node. In this case, try prev()

0
Ruslanas Balčiūnas On
$('.editAnswer').parent().find('.answer_id').val('value');