HTML
<input type="text" id="prva">
<input type="text" id="brojOcena">
<input type="button" value="Izracunaj" onclick="Racunaj()">
JS
function Racunaj(){
let prva = document.querySelector('#prva');
let broj = parseInt(document.querySelector('#brojOcena'));
broj = broj.value;
prva = prva.value;
}
How can I get let prva
as prva = 15/broj
instead of "5 5 5" / broj
CodePudding user response:
function Racunaj(){
var prva = document.querySelector('#prva');
var broj =document.querySelector('#brojOcena');
prva.value = eval(parseInt(prva.value)) / parseInt(broj.value);
}
Use parseInt()
to convert string to integer
And just you want answer like that
prva.value = eval(parseInt(prva.value)) / parseInt(broj.value);
//prva = 15/broj
eval()
use to run string code know as integer add.
Try This
CodePudding user response:
You should do
let broj = document.querySelector('#brojOcena');
broj = parseInt(broj.value);
instead.
Referring to the update of your question:
The selector #prva
will only ever get you one DOM element, as id
s must always be unique. So 5 5 5
will not be possible using id
. Maybe you should use a class attribute instead?
If, on the other hand, you want to calculate a formula entered into the input field you could use the (insecure) eval()
function.