I have looked at numerous questions to this problem, using parent, child, sibling, closest etc and I cannot get any to work.
I have a list and each list has a button when I click on the button I want to display text inside the nearest span next to the button.
I just cannot get it to work, as either, the text is appended to all spans or none.
My last attempt is below, so any help would be appreciated
<div class="apply-message">
<span class="need-to-register text-danger"></span>
<div class="d-flex flex-row justify-content-between">
<a href="url" class="btn job-details" type="button">Job Details</a>
<a href="#" class="btn apply apply-search-button" type="button">Apply</a>
<input type="hidden" class="pageId" name="pageId" value="123" />
</div>
</div>
<div class="apply-message">
<span class="need-to-register text-danger"></span>
<div class="d-flex flex-row justify-content-between">
<a href="pageurl" class="btn job-details" type="button">Job Details</a>
<a href="#" class="btn apply apply-search-button" type="button">Apply</a>
<input type="hidden" class="pageId" name="pageId" value="123" />
</div>
</div>
My JQuery is below
$(".apply-search-button")
.closest(".apply-message")
.find(".need-to-register")
.append(`<p>${data.message}</p>`)
.show();
So basically I have the div apply-message and inside that, I have the span need-to-register with the closest button apply-search-button which when clicked such append the message to the closest span and not all spans
*****************Full JQuery**********************
$(function() {
$(".need-to-register").hide();
$(".apply-search-button").on("click", function(event) {
event.preventDefault();
var pageId = $(this).closest(".latest-jobs").find(".pageId").val();
jQuery.ajax({
type: "Post",
url: "/Umbraco/Surface/Members/Apply/",
data: { page: pageId },
cache: false,
dataType: "json",
success: function(data) {
if (data.Confirm === true) {
$(this).closest(".apply-message").find(".need-to-register").append(`<p>${data.message}</p>`).show();
} else if (data.isJsonErrorList === true) {
//$(this).siblings(".need-to-register").append(`<p>${data.message}</p>`).show();
$(this).closest(".apply-message").find(".need-to-register").append(`<p>${data.message}</p>`).show();
} else {
$(this).closest(".apply-message").find(".need-to-register").append(`<p>${data.message}</p>`).show();
}
$("#spinner").hide();
}
});
});
});