简体   繁体   中英

get text of second child in div - jQuery

I'm trying to create a kind of a online shopping cart My problem is that I can't get the price of a single product showing in the shopping cart.

So here is my code

<div id="shoe_one">
  <img src="imgs/img_0.JPG" id="training_shoe" alt="purple adidas shoe" width="150" class="product">
  <p>Adidas Courtset</p>
  <p>Preis: 59,90 €</p>
  <input type="button" value="Warenkorb" class="button">
</div>

my js code:

let selectedItem = $(this).parent();

if(!contains) {

   //let price = selectedItem.("nth-child(3)").text();

   let addedProduct = $("<div class='newProduct'></div>");
   addedProduct.append(selectedImage.clone());
   addedProduct.append("<p>Anzahl: " + amount + "</p>" + "<p>" + price + "</p>");
   cart.append(addedProduct);
   content.append(cart);
}

As your HTML stands, if you want to get the price text on clicking the button then you can target the previous p element.

Demo:

 $(".button").click(function(){ let selectedItem = $(this); let price = selectedItem.prev("p").text(); console.log(price); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="shoe_one"> <img src="imgs/img_0.JPG" id="training_shoe" alt="purple adidas shoe" width="150" class="product"> <p>Adidas Courtset</p> <p>Preis: 59,90 €</p> <input type="button" value="Warenkorb" class="button"> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM