0
i need help to fixed my code work. it shows to me TypeError :cannot set properties of null(setting 'onclick'
window.onload= function(){ let btn = document.getElementById("buybtn"); btn.onclick = function(){ let adult= getElementById("adult").value; let children= getElementById("children").value; let price = calc(adult, children); alert(price); } }
7 Respostas
+ 3
in html you have id="adult"
but in Javascript you are referring to "adults" plural. make them identical .
you also need to parse values as int or float . otherwise your function will just concatenate the two values . like 5+4 = 54 because they are treated as strings.
I added parseInt()
here's the fixed code
https://code.sololearn.com/W2XS5A3nGDIE/?ref=app
+ 2
your js code is not complete .
you used let adult = getElementById...
and let children = getElementById ...
you forgot "document"
it should be document.getElementById
the function calc() is not defined anywhere in the code . so it will not work .
+ 2
Thank you very much Bahha🐧 code is run now without error.
Again thank you for your explanation.😍
+ 1
Check if the id attribute in html is set to buybtn
0
<form id="buy">
<div class="form-section">
<label for="adult">Adults:</label>
<br>
<input type="number" id="adult">
</div>
<div class="form-section">
<label for="children">Children:</lable>
<br>
<input type="number" id="children">
</div>
<div class="form-section">
<input type="button" value="Buy" id="buybtn">
</div>
</form>
above is my html code belongs to form and button please let me any error?
0
<form id="buy">
<div class="form-section">
<label for="adult">Adults:</label>
<br>
<input type="number" id="adult">
</div>
<div class="form-section">
<label for="children">Children:</lable>
<br>
<input type="number" id="children">
</div>
<div class="form-section">
<input type="button" value="Buy" id="buybtn">
</div>
</form>
here is my html code if you find error please let me i will fix.
0
function calc(adults,children){
return adults+children;
}
price = calc(adults*12,children*5);
calc();
window.onload= function(){
let btn = document.getElementById("buybtn");
btn.onclick = function(){
let adults= document.getElementById("adults").value;
let children= document.getElementById("children").value;
let price = calc(adults, children);
alert(price);
}
}
still not work my code. thanks for remind me missing word "document"