Domanda

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>
È stato utile?

Soluzione

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

   var answer_id = $('.editAnswer').parent().siblings('.answer_id').val();

Altri suggerimenti

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

$('.editAnswer').parent().find('.answer_id').val('value');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top