Reputation:
I have a simple problem with JQuery.
I have lots of buttons that will be dynamically generated with JSON feed and stuff that doesn't matter for the question.
but with every item from the feed, it will generate something like a card with a button and a hidden <p>
with the ID of this item, simplifying its something like:
<div>
<h3>ANA DOE<h3>
<!-- this p haves the id -->
<p hidden>001</p>
<a class='btn green-meadow btn-sm AceitaVistoria'>ACCEPT</a>
</div>
So, when I click the button, I need to take the id of the hidden p to use latter, here is what I'm doing:
$('.AceitaVistoria').click(function (newEvents) {
var idVistoria = $(this).prev().text();
});
but it's not working, what can I do?
Upvotes: 0
Views: 72
Reputation: 576
Try this out. Should work.
$('.AceitaVistoria').click(function() {
alert($(this).prev().html());
});
<div>
<h3>ANA DOE<h3>
<!-- this p haves the id -->
<p hidden>001</p>
<a class='btn green-meadow btn-sm AceitaVistoria'>ACCEPT</a>
</div>
Upvotes: 0
Reputation: 1237
This is propably it (keyword: dynamicaly, therefore do not exist on page creation and never get the on click listener)
$(document).on("click",'.AceitaVistoria',function (newEvents) {
var idVistoria = $(this).prev().text();
});
Upvotes: 2
Reputation: 2114
Why don't add the id
as an attribute of the link?
<div>
<h3>ANA DOE<h3>
<!-- this p haves the id -->
<a data-id="001" class='btn green-meadow btn-sm AceitaVistoria'>ACCEPT</a>
</div>
And then:
$('.AceitaVistoria').click(function (newEvents) {
var idVistoria = $(this).data('id');
});
Upvotes: 6