thats a school's exercise i have to do, but i don't know why it doesn't work. could someone help me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

thats a school's exercise i have to do, but i don't know why it doesn't work. could someone help me?

so the exercise asks to make a sum of 2 numbers using the forms, and wants that those 2 numbers are shown in an alert using a button(i don't know why but can't use the 'submit') here is a picture of how the exercise is supposed to look: https://ibb.co/Wz3jx5z ( it's wrote in italian so i'm going to translate: uno, due, .. , cinque simply means 1,2, .., 5. while "invia il" mens "send the"). here is the code that i have made: https://code.sololearn.com/WA14A10A2381/# , the function or the button doesn't work. which is the problem?

14th Mar 2021, 2:12 PM
Massimiliano Messina
Massimiliano Messina - avatar
9 Answers
+ 2
first error is syntax of 'function' wich should not be capitalized... however, there are some logical errors: id are supposed to be unique... in case of multiple elements with same id, only the first one is returned, so you'll always get 1 and 1... as string (so result of n1+n2 is '11'). you need to get (and cast to number) the select value for n1, and the #N2:checked value for n2... using querySelector will help: https://code.sololearn.com/WA23A22a135A/?ref=app
14th Mar 2021, 2:31 PM
visph
visph - avatar
0
Thanks visph but how does the .querySelector works?
14th Mar 2021, 2:53 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
querySelector get one string argument wich is the css selector string of targeted element... it return the first element matching it. querySelectorAll works same, but return an array-like of all matching elements... both are available on document and every node elements ;)
14th Mar 2021, 2:56 PM
visph
visph - avatar
0
visph and select and checked? Couldnt write in the First One "#N1:checked"? edit: maybe i understand, the queryselector automaticcally takes the First id so for add the second id to the variable Need to use the "#N2:checked"
14th Mar 2021, 3:00 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
no: :checked pseudo class is only available for radios/check boxes... select works differently, and hold selected value directly on select element ;)
14th Mar 2021, 3:04 PM
visph
visph - avatar
0
visph thanks for everything at school they basically give tasks without explaining that much, you been a lot more helpful than my teacher
14th Mar 2021, 3:06 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
you're maybe expected to do some research by your own ;P however, I don't know if you are supposed to use anything else than simple <input type="text" id="Nx"> (or type="number"), from wich you could get value just by +getElementById('Nx').value ^^
14th Mar 2021, 3:12 PM
visph
visph - avatar
0
visph i must use the radio and the other type I don't remember right now
14th Mar 2021, 3:23 PM
Massimiliano Messina
Massimiliano Messina - avatar