+ 2

Dynamically changing Select tag options

I'm creating a website that allows a user to save a parking spot in the selected state. In the website I have a <select> that presents all the states in the U.S. then another <select> that contains all the cities in the state. My question is how do I have the 2nd <select> change it's options based on the option selected in the 1st <select> using html and JavaScript? I would appreciate any help, thanks in advance.

30th Jul 2021, 12:30 AM
james vanmeter
james vanmeter - avatar
7 Respostas
+ 6
Here is an example of how you can do that with jQuery: https://code.sololearn.com/WA11A3a7312A
30th Jul 2021, 12:36 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 4
https://code.sololearn.com/W6rqi00oXPVn/?ref=app
30th Jul 2021, 11:36 AM
Calviղ
Calviղ - avatar
+ 4
James Vanmeter, Try using "element.val('val')" to populate the second <select>
31st Jul 2021, 12:26 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 4
I don't use VSC, so I got nothing to say about it. Try to load the code in a standard web browser to see whether either one of the codes posted here works. If it works as expected, then *perhaps* it has something to do with VSC. Though, I kinda doubt VSC will be having problem processing basic codes like these.
31st Jul 2021, 3:16 PM
Ipang
+ 3
I tried all of these examples and I'm still having the same issue, but now I'm pretty sure it's not the code. I'm developing in visual studio code, when I tried using the code everyone presented, I copied and pasted to visual studio just to see if it would work. 1st <select> always works, 2nd <select> remains blank no matter what. I know my JavaScript is linked correctly because my other functions are working. What do you guys think I'm wrong?
31st Jul 2021, 12:05 PM
james vanmeter
james vanmeter - avatar
+ 3
james vanmeter try to git push the code to GitHub, and share the git clone link here. So that we can reproduce your problem in our VSC.
31st Jul 2021, 5:29 PM
Calviղ
Calviղ - avatar