Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I am a lost little bird:(

I can not get my JavaScript to display the tip. HTML

            <div class="all" id="pro8"><!-- Tip, Tax, and Total -->
                <h1>Tip, Tax, and Total</h1>
                <form id="tipTaxTotal">
                        <label for="price">Price:</label>
                        <input type="text" name="price" id="price">
                        <label for="tip">Tip:</label>
                        <input type="text" name="tip" id="tip">
                        <label for="tax">Tax:</label>
                        <input type="text" name="tax" id="tax" readonly>
                        <label for="total">Total:</label>
                        <input type="text" name="total" id="total" readonly>
                </form>
                <button type="button" class='tipTaxTotal' id="calculate8">Calculate</button>
                <button type="button" class='tipTaxTotal' id="clear8">Clear</button>
            </div>

JavaScript

let price = document.querySelector('input [name="price"]');
price = parseInt('price');
let tipD = document.querySelector('input [name="tip"]');
let taxD = document.querySelector('input[name="tax"]');
let totalD = document.querySelector('input[name="total"]');
document.addEventListener("click", x => {
    if ((x.target.tagName = "BUTTON")) switch (x.target.id) {
        case 'calculate8' : let tip = price * .15; let tax = price * .07; let total = price + tip + tax;
        tipD.textContent = tip.toString('m'); break;
    }
});

3 Answers

Steven Parker
Steven Parker
243,656 Points

You do have a few issues here:

  • use no space between the tag name and the bracket in a property selector
  • never put a variable name in quotes
  • you can't read the value from the box before the button event
  • you probably don't want to use the same variable for an element and a value
  • to get the value from an input you take the ".value" property
  • to put a value into an input you also use the ".value" property (not "textContent")
  • didn't you point out to me (in a previous question) that the "if" doesn't need double parentheses?
  • "m" is not a valid radix for "toString"
  • you don't need "toString" anyway, automatic type conversion will handle it

look, I think I found it :point_right: :bird: :exclamation:

ha ha Thanks a ton man. I don't know why I am having such a hard time with JavaScript.

Hey man I am not understanding bullet 3. and I am not seeing where I am in violation of bullet 4. sorry to keep taking your time. I really appreciate all of your help.

Steven Parker
Steven Parker
243,656 Points

They are both the 2nd line...

price = parseInt('price');

It doesn't work because you've quoted the name (and forgot the ".value" property), but you're trying to sample (and convert) the value of the "price" box here during startup. At that time, the box is surely empty. But when the button is pushed, the user will have put something in it.

And you named the input "price" but you're using it here for a value. You might want to name the input "priceD" like the other boxes.

Steve you are the man thanks again for all f your help it is working like a dream now. sorry I thought I hit sent on this last night.