i want to access value of the two input to make calculation immediately they put in a value
<script>
//i want to access value of the two input to make calculation immediately they put in a value
const heightInputEl = document.querySelector('.height-input');
const weightInputEl = document.querySelector('.weight-input');
heightInputEl.addEventListener('input', function (e) {
const height = Number(e.target.value);
const mtrsqr = Math.abs(Math.pow(height / 100, 2));
});
weightInputEl.addEventListener('input', function (e) {
const weight = Number(e.target.value);
});
</script>
2
Answers
By defining mtrsqr and weight outisde the event, we can access them anywhere
Your code is probably not running because it ran even before the said html elements are loaded. Try setTimeout and it should work perfectly as demonstrated in this example using your code – https://jsfiddle.net/hiyer/sL18nqe7/36/