0

How can I pass value from html input to javascript I have value_1 = 0 but when I change it in input form I want code inside if to happen.

<body>
<input type="number" name="a" id="commands"><br>
<canvas id="myChart1"></canvas>

<script>
const value_1 = 0;
const value_1 = document.getElementById("commands").value;

if(value_1 != 0)
{
 //code
}
</script>    
</body>
3
  • 1
    So you need to add an event listener. You just check it when the page loads. Commented Oct 22, 2021 at 17:01
  • 1
    You will need to associate an event listener with the input; see documentation: developer.mozilla.org/en-US/docs/Web/API/EventTarget/… Commented Oct 22, 2021 at 17:04
  • Be aware, in your code you are trying to reassign a constant value, this should throw an error. In your case you could skip the line const value_1 = 0; entirely because one line later you reassign the value of value_1 Commented Oct 22, 2021 at 17:16

4 Answers 4

1

add a listener for the 'keyup' event, as follows:

document.getElementById("commands").addEventListener('keyup', (e) => {
   let value = document.getElementById("commands").value;
   if (value !== 0) {
      // code
   }
});

Or like this:

document.getElementById("commands").onkeyup = function(e) {
   let value = document.getElementById("commands").value;
   if (value !== 0) {
      // code
   }
}

or with jQuery:

$('#commands').on('keyup', (e) => {
   let value = $('#commands').val();
   if (value !== 0) {
      // code
   }
});
Sign up to request clarification or add additional context in comments.

2 Comments

I would recommend using !== instead of !=
You're right, it's better. I just copied the original snippet of the interviewer's code. @thomas
0

You can use addEventListener with change event type or use the onchange property.

Example:

    <script>
    const value_1 = document.getElementById("commands");

    value_1.onchange = function(){
        if(value_1.value != 0)
        {
            console.log("Not 0");//code
        }
    }
    </script> 

Comments

0

If I understood,

To track input value changes you could do so by:

  1. Initialize the variable:
var inputValue = "";
  1. Listen for input changes (There are multiple ways to do it, I will use the one which listen whenever user clicks button on the input):
document.getElementById("commands").addEventListener("keyup", function(event) {
  inputValue = document.getElementById("commands").value;
  
  // Here you can do anything, incase you need to have a callback whenever the value changes.
  // Example:
  if(inputValue != 0) {
    // code.
  }
});

Comments

0

You can pass value from html to JavaScript correctly using following code:

HTML:

<form onsubmit="return getValue('commands')">
  <input type="number" name="a" id="commands">
  <canvas id="myChart1"></canvas>
</form>

JavaScript:

<script>
    function getValue(id) {
        let value_1 = 0;
        value_1 = document.getElementById(id).value;
        alert(value_1);
        
        if(value_1 != 0)
        {
         //code
        }
        return false;
    }
</script>

Comments

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.