Jump to content

Hi Guys! I have coded some code just to test the radio buttons that shows in console which one is selected but when i reload the page it automatically logs to the console first one before actually selecting it. And after that i select other options but nothing happens. Can you tell me where's the problem. Here i will atach the code. Thank you!!

index.html

Link to comment
https://linustechtips.com/topic/1020586-html-radio-button-problem/
Share on other sites

Link to post
Share on other sites

Hello,

 

There are a few separate issues, the reason that it logs to console as soon as you load the page is that at the end of your provided script you are calling the `check()` function that you created. This means as that script file is parsed during load into the browser the check function is called, resulting in that initial log.

 

When you then hit the radio buttons you do not see any further logs, this is because the pressing of the radio buttons is not set to call any of your code. You need to register an event listener for changes to the state of the radio buttons to then call your `check()` function.

 

There is also an issue with the equality tests that your doing in the check function, this is a very common mistake that people new to JavaScript make as the `=` your using is setting the state of the radio button, not comparing it. For this you need to use `===`. For a better explanation see the link below:

https://www.codecademy.com/en/forum_questions/558ea4f5e39efed371000508

 

I have added the event listener you need and identified the bugs, the updated code is available below: 

https://gist.github.com/Ro5635/08150f840560dc646b71f90243c91814

 

Good luck,

 

Robert :) 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×