Luth Hizb Luth Hizb - 2 months ago 6
Javascript Question

Javascript trigger without button click

i have some problems here..
I have an input that should be multplied by 3 without button

This is my code

<input type="number" placeholder="Input a number" id="number"
onclick="multiply()">

<div id="result"></div>


This is my script

<script>
function multiply(){
var num = document.getElementById('number').value;
var res = document.getElementById('result').innerHTML;

res = num * 3;

}
</script>


So here i want user can input the number and get the result immediatelly without button..

Thanks..

(Beginner Programmer)

Answer

Remove the inline onclick handler and subscribe to the input event instead;

var res = document.getElementById('result');

document.getElementById('number').addEventListener('input', function() {
    res.textContent = this.value * 3;
}, false);

var res = document.getElementById('result');

document.getElementById('number').addEventListener('input', function() {
  res.innerHTML = this.value * 3;
}, false);
<input type="number" placeholder="Input a number" id="number">

<div id="result"></div>

Comments