简体   繁体   中英

Get value from input field while typing, using javascript

I am trying to set 2 variables with values from input fields and use them to calculate area.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<form>
  <label for="fname">Height:</label>
  <input type="text" id="boxHeight" name="fname"><br><br>
  <label for="lname">Width:</label>
  <input type="text" id="boxWidth" name="lname"><br><br> 
</form>
</body>
</html>
//var l = jQuery('#boxWidth').find('input');   ---> does not work
//var h = jQuery('#boxHeight').find('input');   ---> does not work
var l = document.getElementById("#boxWidth");
var h = document.getElementById("#boxHeight");

var a = parseInt(l);
$(document).on("change paste keyup", "#boxWidth, #boxHeight", () => {
                console.log("text "+a);
            });

The code can be tested here get input to integer

Nothing seems to work. Any suggestions?

You have a number of small issues getting the right input, and the value from those inputs.

$(document).on("change paste keyup", "#boxLatime, #boxInaltime", () => {
  var boxLatime = document.getElementById("boxLatime");
  var boxInaltime = document.getElementById("boxInaltime");
  var l = parseInt(boxLatime.value) || 0;
  var h = parseInt(boxInaltime.value) || 0;
  vr_do_l_h(l, h);
});

https://jsfiddle.net/dvtc9brq/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM