jquery - Select text within h4 -


how grab text 'peanut brownie' when button pushed.

 <div class="col-sm-4 col-lg-4 col-md-4">     <div class="thumbnail">         <img src="images/peanutbrownie320x150.jpg" alt="afghan">         <div class="caption">             <h4 class="pull-right">$12.00</h4>             <h4>peanut brownie</h4>             <p>one dozen (12)</p>                 <div class="row">                     <div class=".col-md-8 addtocart">                     <button type="button" class="btn btn-primary">add cart</button>                     </div>                 </div>         </div>     </div> </div> 

i have tried ...

$( "button" ).on( "click", function()   {  alert( $(this).closest('h4').text() )  }); 

and few other tricks empty alert.

you closest div has h4, , last h4 child of div

$( "button" ).on( "click", function()   {    alert( $(this).closest('div:has(h4)').children('h4:last-of-type').text() )  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="col-sm-4 col-lg-4 col-md-4">      <div class="thumbnail">          <img src="images/peanutbrownie320x150.jpg" alt="afghan">          <div class="caption">              <h4 class="pull-right">$12.00</h4>              <h4>peanut brownie</h4>              <p>one dozen (12)</p>                  <div class="row">                      <div class=".col-md-8 addtocart">                      <button type="button" class="btn btn-primary">add cart</button>                      </div>                  </div>          </div>      </div>  </div>


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -