I am very new to programming, but I have some experience with HTML and CSS. I decided I would start learning to program with javascript because it seemed like the logical next step.
I have been through tutorials on LyndaDotCom and Udemy, and they were great, but they seem to only cover the basics of syntax and defining the basic terms. I learned how to write simple functions, loops, define variables…etc. I decided to come up with a project on my own to get some practice in and I ran into a problem. I created a form with two dropdown menus and some radio buttons. I have been looking for an explanation of how to take the selected user input from these and place the input into variables.It seems like this would be widely available in forums, but I’m having a really hard time locating the answer.
I know how to use document.getElementById() to access the dropdown, but how do I take the user’s selection and place it into a variable?
For the radio buttons, I know I can use the document.getElementsByName() to get the items, but how do I take the selected item and place it into a variable?
It seems like it should be pretty easy, but I haven’t been able to find an easy example, and certainly none that some with a good explanation of how it works.
Lots of various approaches, so an example of what you’re wanting to do would help. But for a simple example:
Assuming an input won’t have a user supplied value until a user does something, you will likely want to assign an event handler - change, blur, submit, etc.
Then you assign the value to a variable. eg. var input_val = document.getElementById('some_input_id').value;
So that is the question. If I’m understanding you correctly though, to do this I need and event handler?
After “handling” the event, I can then place the selection into a variable?
If that is the case, I will start researching how to create an event handler.
Thanks for the help, guys. I have the dropdown menu selection working perfectly, and I have written the code a number of times with different menus so I will understand and remember it.
The radio buttons are still giving me trouble. I will keep working on it, though.