user1059511
user1059511

Reputation: 267

Get text from span above link - Jquery

Hi I am trying to get the text from a span element directly above a link.

$(this).closest("span").text()

Full Code:

JS

$boxes.find('.portlet-header .portlet-maximize').click(function() {
// returns the rel of the link fine   
var widHtml = $(this).attr('rel'); 
// get text from span above link (not working)
var widTitle = $(this).closest("span").text()
});

HTML

<div class="portlet-header">
<span class="widTitle gmIcon">text I want to get</span>
<a rel="widgets/dashboard/max/goal-mouth" class="portlet-maximize"></a>
</div>

Upvotes: 2

Views: 1385

Answers (1)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262939

closest() walks the ancestor tree. Since your <span> element is a sibling of the anchor, not an ancestor, you should use prev() instead:

var widTitle = $(this).prev("span").text();

Upvotes: 4

Related Questions