Jquery tree traversing not selecting
-
15-04-2021 - |
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> · <a href='#' class='downVote vote'>Downvote</a> ·
<a class='answerTime'> $difference $periods[$j] ago</a>
· <a href='#' style='color: orange;' class='editAnswer'><b>Edit</b></a>
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