2

I'm getting two value from input type number but result not showing well

const price = document.querySelector(".price").value;
const items = document.querySelector(".items").value;
const a = price + items;
const subtotal = document.querySelector(".total");
subtotal.addEventListener('click', function(){
    console.log(a);
});
6
  • 1
    const a = Number(price) + Number(items); Commented Jul 15, 2019 at 14:09
  • What does the html look like, and what numbers are you inputting? Also, what is the final logged output? Commented Jul 15, 2019 at 14:09
  • 1
    You know you are reading the values when page loads, not when it is clicked. The code above the click does not magically update. Commented Jul 15, 2019 at 14:10
  • This should be easy to put into a runnable snippet to demonstrate the problem. Would you be willing to do that? Commented Jul 15, 2019 at 14:11
  • "but result not showing well" Please elaborate on the problem and show us any errors you're getting Commented Jul 15, 2019 at 14:12

3 Answers 3

2

The reason you get a dodgy output is because of 2 reasons:

  1. You get the values once and only once, so updated values will be incorrect when you call the function.

  2. You are concatenating strings together, rather than adding strings. E.g: '5' + '2' = '52'

You should get the values inside the function, because they may change, and you should convert the values to numbers before adding them. You should also use ID's - not classes - for specific elements:

const subtotal = document.getElementById("total")

subtotal.addEventListener('click', function() {
  const price = +document.getElementById("price").value,
    items = +document.getElementById("items").value,
    a = price + items
  console.log(a)
});
<input type="number" id="price" value=5>
<input type="number" id="items" value=2>
<button id="total">Add</button>

You can write this function better like so:

const subtotal = document.getElementById("total")

subtotal.addEventListener('click', () => console.log(['price', 'items']
  .reduce((a, id) => a + +document.getElementById(id).value, 0)
))
<input type="number" id="price" value=5>
<input type="number" id="items" value=2>
<button id="total">Add</button>

With this approach, you can just add an ID to the list to add that element too.

Sign up to request clarification or add additional context in comments.

2 Comments

querySelector() will return the first matched element, there is nothing wrong with document.querySelector(".price").value
@Mamun yes, you're right, I forgot about that, but it is better practice to use ID's
1

Though the type of the control is number, the actual input is of type string. Hence string concatenation (i.e, '2' + '3' = '23') is happening. You have to convert the values to number before performing the addition. To get the current value from the controls you also have to access the values inside the function:

const subtotal = document.querySelector(".total");

subtotal.addEventListener('click', function(){
  const price = document.querySelector(".price").value;
  const items = document.querySelector(".items").value;
  console.log(typeof price); // string
  const a = Number(price) + Number(items);
  console.log(a);
});
<input type="number" class="price"/>
<input type="number" class="items"/>
<button type="button" class="total">Total</button>

Comments

-2

You must do all that logic inside the event listener so that you can read input values even if they changed and calculate result based on current input values.

It's also good to declare a function for sum so you can reuse it wherever you need it.

function sum(a,b) {
    return Number(a) + Number(b);
}

const btn = document.querySelector(".btn");

btn.addEventListener('click', function(){

    const price = document.querySelector(".price").value;
    const items = document.querySelector(".items").value;

    document.querySelector(".total").innerText = sum(price, items);
});
<input type="text" class="price">
<input type="text" class="items">
<button class="btn">SUM</button>
<div class="total"></div>

4 Comments

Your code doesn't work as it should. Also, I see no need to have a sum function. It takes more characters to write sum(4, 3), than it does to write 4 + 3.
Thanks @Kobe there was a bug. About the function, I was not trying to solve the problem using the minimum amount of characters as possible but making it the best as possible.
How is using a sum function any better than writing it out normally?
I already said in my answer, a function can be reused later in the code. I'm thinking about an wider scenary than the one displayed in the question which would be probably the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.