2021年3月20日星期六

Jquery - How to access child of parent from a separate child [closed]

I am working on an assignment and we are exploring jquery for the first time. I am trying to access the an element up a parent then down a seperate child to get a class and print its text.

The initial class is referenced with closest because I want the answer to be different depending on which button you press.

Here is the code so far

$(document).ready(function() {      $('button').on('click', function() {          var destination = $(this).closest('.destination');          var base_price = $(destination).find('.base_price').text;          $(this).remove();          console.log(base_price)          });      });  

I also tried using just one variable - $(this).closest('.destination').find('.base_price').text;

I tried several other combinations of parent() and child()

HTML

<div class = "destinations">      <h2>New York, New York</h2>      <img src="images/newyork.jpeg">      <p>          Includes: 5 Night Hotel Stay, Central Park Tours, and New York Skyscrapers Package<br>          Starting at $<span class="base_price">1299.99</span>      </p>      <div>          <h3>Add-Ons</h3>          <input type="checkbox" id="option1" name="option1" value="1000">          <label for="option1"> Add Two-Night Stay at the Ritz</label><br>          <input type="checkbox" id="option2" name="option2" value="200">          <label for="option2"> Broadway Show</label><br>          <input type="checkbox" id="option3" name="option3" value="400">          <label for="option3"> New York Steakhouse Gourmet Night Out</label><br>      </div>      <div>          <button id = "nyny">Get price</button>      </div>  </div>  

This is just one example, but basically I could add as many destinations as I wanted but depending on what button you press you get the associated price.

I would like to also get add extra to the price based on checkbox selection, but I figured the method will probably be similar to this once I figure it out. The final print out will be something like ("Estimate Price: $"(base_price + add_ons * 1.075) + " (Includes 7.5% excise tax)")

console.log is returning: ƒ (e){return z(this,function(e){return void 0===e?w.text(this):this.empty().each(function(){1!==this.nodeType&&11!==this.nodeType&&9!==this.nodeType||(this.textContent=e)})},null,e,arguments.length)} Which I have no idea what it means and didn't pull anything when I looked it up.

Any help on this will be greatly appreciated! Let me know if this is the wrong direction for my goals as well, especially wanting to access checkbox values later.

Thank you

Kevin

https://stackoverflow.com/questions/66727587/jquery-how-to-access-child-of-parent-from-a-separate-child March 21, 2021 at 07:50AM

没有评论:

发表评论