Attila Naghi
Attila Naghi

Reputation: 2686

How to get the previous text value of a div by clicking a html item?

In the image below is the HTML code: enter image description here

and this is my js code:

$(".salveazaTransaSuma").click(function(){
   var payment_name = $(this).siblings().previous(".transaContentEdit").text();
   alert(payment_name);
});

So I want to get the text value from the transaContentEdit class, when i click on the salveazaTransa class. Don't Worry about the white spaces before and after the text value ("Servicii programare Transa 1"). Appearantly my code doesn't work. It gives me an error in firebug "TypeError: $(...).siblings(...).previous is not a function". Any idea how to hande this ?

Upvotes: 0

Views: 49

Answers (3)

Adil
Adil

Reputation: 148110

You need prev not previous

$(".salveazaTransaSuma").click(function(){
   var payment_name = $(this).closest('div.litrans.suma').prev().find(".transaContentEdit").text();
   alert(payment_name);
});

Upvotes: 1

monu
monu

Reputation: 370

previous is not a function use prev.

var payment_name = $(this).closest('.litransa').prev(".transaContentEdit").text();

Upvotes: 1

Felix
Felix

Reputation: 38102

Based on your image, you can use:

$(".salveazaTransaSuma").click(function(){
   var payment_name = $(this).closest('.litransa').prev().find(".transaContentEdit").text();
   alert(payment_name);
});

Upvotes: 1

Related Questions