Javascript Select

| | | | | | | | | | | | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

As you will learn how to create web forms, one of the most common tasks is getting values ​​from drop - down menus. In this article, we are going to learn how to get these values ​​with JavaScript.

Let’s take a quick rundown on how to create a dropdown element using HTML. Follow my Codepen if you get lost.

to build our drop down element we need to use the select and use the option children nested below. The selection element creates the drop-down menu and with the option tags we set these options in the drop-down menu live on.

Let’s create a dropdown menu of our favorite fruits (which have emojis).

we have added an empty h2 that we will fill in with JavaScript later. At this point you can choose your favorite fruit:

92151925 81da0a00 Ede7 11EA 9796 2a8a506c4a6e

Here we access the attribute options d ’ array element. Next, we choose the index of the selected option and access its value or text.

Now try to console.log both variables. What’s going on? Nothing. Why? Because our first element has an empty value (it’s just a placeholder). Give your opinion on the first option and try again. Yay! We get the value and the text with the javascript.

Listening for drop down menu changes with javascript < / h2>

We need to make our dropdown menu dynamic because JavaScript is designed to react to changes on your page. With that in mind, we’re going to assign an eventListener to our fruit element.

With our eventListener we hear a change in our drop-down menu. Note that you can also get the value of the selected option by running e.target.value, which you probably saw in another tutorial module.

we are here. You can now select a fruit option and JavaScript will react to the changes in our drop-down menu and update our h2 accordingly. Successful Law

92155152 88b74b80 Edec 11EA 8016 7f8911f26735

Conclusion

How unfolding values ​​is an essential skill as you learn web development . the values we get are important if we want to update a basic data show se lection to the user or just "react" to changes in the DOM. With JavaScript we can get these values ​​in no time

< / div> < / div>

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically