sqarf
sqarf

Reputation: 347

Jquery select div within common parent

Consider code:

<div class="wrapper">    
    <div class="target">        
        <div class="sub1">
            <span class="value">
                10
            </span>
        </div>            
        <div class="sub2">
            <button type="button">Trigger</button>
        </div>            
    </div>

    <div class="target">                
        <div class="sub1">
            <span class="value">
                20
            </span>
        </div>            
        <div class="sub2">
            <button type="button">Trigger</button>
        </div>            
    </div>    
</div>

How to select following value. If I click on 'button', I want jquery to select parent div class="target" and get span class="value" .text().

The trick is to not select direct parent, but to be able to reach multiple level parent

Upvotes: 1

Views: 75

Answers (1)

The Process
The Process

Reputation: 5953

You can do it like this:

$('button').on('click',function(){

  var text=$(this).closest('.target').find('.value').text();

});

Upvotes: 2

Related Questions