Search code examples
javascriptjquerygetvalue

Javascript & jquery | I want to get the text in the <span> tag outside the button the user clicked on


Web site picture

Hello I want to get a val in span with on click event but I must use one event for this.

What am I trying to do basicly if user click PAY button inside in second div I should get

.faturatarih value witch is "09-05-2021 10:22" in second div but

if user click PAY button in first div I should get "09-05-2021 08:22"

sorry for my bad english

Here is my HTML code

<div class="fatura">
    <span class="faturaTarih">09-05-2021 08:22</span>
    <button type="button" class="faturaOdeButton">PAY</button>
    <p class="faturaGonderenIsım">Mr. Jack</p>
    <span class="faturaAciklama">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quae, minus provident.</span>
    <span class="faturaDurum">Waiting</span><span class="faturaUcret">$500,00</span>
</div>
<div class="fatura">
    <span class="faturaTarih">09-05-2021 10:22</span>
    <button type="button" class="faturaOdeButton">PAY</button>
    <p class="faturaGonderenIsım">Mr. Tom</p>
    <span class="faturaAciklama">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quae, minus provident.</span>
    <span class="faturaDurum">Waiting</span><span class="faturaUcret">$1000,00</span>
</div>

Solution

  • you can try this:

    $(".faturaOdeButton").on("click", function()
    {
    
    // Get date
     var date = $(this).closest(".fatura").find(".faturaTarih").text();
     
     // For test
     alert(date)
     
    })