0

I need to store value from input in array and after that, use it. Help please =)

<body>
  <p></p>
  <form>
    <label for="cr"> <input type="number" id ='cr'> </label>
    <button>Submit</button>
  </form>
</body>

const inputCr = document.querySelector('#cr');

const btn = document.querySelector('button');

  let allCr = [];

const calculate = function(){
  const valueC = Number(inputCr.value);
  allCr.push(valueC);
}
btn.addEventListener('click', calculate());
4
  • You are on the right path, but why do you want to put it in an array? Commented Oct 1, 2022 at 11:29
  • Why do you say you can't use your array ? I'm not sure to understand what is giving you trouble. Commented Oct 1, 2022 at 11:47
  • @ArrowHead Beacuse, there will be at list 5 values, that i needed to add one to another and after calculate sum of the value ) Commented Oct 1, 2022 at 11:48
  • 2
    change the event lister part to btn.addEventListener('click', calculate); Commented Oct 1, 2022 at 11:52

1 Answer 1

1

The problem I can see with your code is the way you pass calculate function to the event listener.

Change

btn.addEventListener('click', calculate());

to:

btn.addEventListener('click', calculate);

Still not sure why you need the array, but this will get you started :)

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

1 Comment

It works, but after I put some value, it appears in a second and then gone.

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.