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); } }

27th Jan 2023, 5:08 AM
Moahamed Azham Mohamed Ashraf
Moahamed Azham Mohamed Ashraf - avatar
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
27th Jan 2023, 9:37 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 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 .
27th Jan 2023, 8:49 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 2
Thank you very much Bahha🐧 code is run now without error. Again thank you for your explanation.😍
27th Jan 2023, 3:00 PM
Moahamed Azham Mohamed Ashraf
Moahamed Azham Mohamed Ashraf - avatar
+ 1
Check if the id attribute in html is set to buybtn
27th Jan 2023, 5:56 AM
MATOVU CALEB
MATOVU CALEB - avatar
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?
27th Jan 2023, 8:33 AM
Moahamed Azham Mohamed Ashraf
Moahamed Azham Mohamed Ashraf - avatar
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.
27th Jan 2023, 8:34 AM
Moahamed Azham Mohamed Ashraf
Moahamed Azham Mohamed Ashraf - avatar
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"
27th Jan 2023, 9:03 AM
Moahamed Azham Mohamed Ashraf
Moahamed Azham Mohamed Ashraf - avatar