-3

Here is my code

<html>
<head>
    head
    <title>title</title>
</head>
<script>
    var val1 = parseInt(document.getElementById('input1'));
    function bytton()
    {
        window.alert(val1);
    }
</script>
<body>
    <br>
    <input type="number" id="input1"/>
    <br>
    <button type="button" onclick="bytton()">value of val1</button>
</body>
</html>

It runs properly without any problem.

At the input field i write a number in it and then click on the button but then it displays that the value is NaN,I think the value is not getting assigned to val1.

Fungus
  • 1
  • 2
  • When do you expect that a value is assigned to `val1`? The `parseInt(document.getElementById('input1'))` isn't part of function that is ran when you press the button. (Also, you should look up how to get the `value` from an element, instead of the element itself.) – Ivar Sep 22 '22 at 13:03
  • 2 notes: (1) You should put the var val1 = .... inside the function, as you want to get the value once the user clicks the button. Otherwise it will always be the initial value (which the input is empty) (2) You should add .value to access the element's input "**value** and not the HTML object. – Ofir Baruch Sep 22 '22 at 13:05
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 22 '22 at 13:05
  • I removed the parseInt part but then now i get that the value is null – Fungus Sep 22 '22 at 13:06

1 Answers1

0

<html>
<head>
    head
    <title>title</title>
</head>
<script>
    function bytton() {    
    window.alert(document.getElementById("input1").value);
    }
</script>
<body>
    <br>
    <input type="number" id="input1"/>
    <br>
    <button type="button" onclick="bytton()">value of val1</button>
</body>
</html>