Callombert
Callombert

Reputation: 1099

Select previous element in jQuery - closest not working

I'm trying to select an element in jQuery but can't do it successfully. I tried prev, prevall and closest but no success.

Basically, when clicking a form, I want to update a value above my form (in a page where the forms will appear multiple times).

I tried making it work in jsfiddle there (one click should return the value above it):

http://jsfiddle.net/s7c2f/1/

This is what I'm trying to do:

$(".addshowscoreinqtip").click(function() {
    var button = $(this);      
    var curscore = button.closest('.rating-recent-text');
    var currentscore = curscore.html();
    alert(currentscore);

 });

Any help appreciated!

Upvotes: 0

Views: 123

Answers (3)

Ram
Ram

Reputation: 144689

closest selects the closest matching parent element and prev selects the direct previous sibling element, you can use both of them:

var curscore = button.closest('.tip-content')
                     .prev('.trending-container')
                     .find('.rating-recent-text');

http://jsfiddle.net/sgN4q/

Upvotes: 2

mishik
mishik

Reputation: 10003

You will need to go up to common parent, .columns in your case. For example:

DEMO

 $(".clickhere").click(function() {
    var button = $(this);      
    var curscore = button.closest('.columns').find('.rating-recent-text');
    var currentscore = curscore.html();
    alert(currentscore);
 });

Upvotes: 0

Anton
Anton

Reputation: 32581

Try this

  var curscore = button.closest('.two.columns').find('.rating-recent-text');

DEMO

Upvotes: 0

Related Questions