Get selected value in dropdown list using JavaScript

Cover Image for Get selected value in dropdown list using JavaScript
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 Hey there, tech enthusiasts! Today, we are diving into a common JavaScript challenge: how to get the selected value from a dropdown list. 🤔 Don't worry, we've got you covered! In this blog post, we will walk you through the process, provide easy solutions, and give you an exciting call-to-action at the end. Let's get started! 🚀

The Problem

So, you have an HTML form with a dropdown list, and you want to retrieve the selected value using JavaScript. Here's an example to illustrate the context:

<form>
  <select id="ddlViewBy">
    <option value="1">test1</option>
    <option value="2" selected="selected">test2</option>
    <option value="3">test3</option>
  </select>
</form>

Solution 1: Using Vanilla JavaScript

The simplest way to grab the selected value is by using plain JavaScript. We can fetch the value using the value property of the selected <option> element. Let's see it in action:

const dropdown = document.getElementById("ddlViewBy");
const selectedValue = dropdown.value;

console.log(selectedValue); // Output: 2

In the code above, we first obtain the dropdown element using getElementById(), providing the ID of the dropdown. Then, with dropdown.value, we retrieve the selected value and assign it to the selectedValue variable. Finally, with console.log(), we can see the value in our browser's console. Easy peasy! 🙌

Solution 2: Using jQuery 🤩

If you're already using the jQuery library in your project, you'll love this solution. jQuery provides a simple and concise way to extract the selected value. Here's how you can achieve it:

const selectedValue = $("#ddlViewBy").val();

console.log(selectedValue); // Output: 2

In the code snippet above, we leverage the power of jQuery by using the val() function. By targeting the dropdown's ID with $("#ddlViewBy"), we efficiently retrieve the selected value. The selectedValue variable captures the result, and console.log() helps us see it in action. jQuery makes life easy, doesn't it? 😍

Conclusion

Now you know two handy solutions to fetch the selected value from a dropdown list using JavaScript. Whether you prefer the elegance of vanilla JavaScript or enjoy the convenience of jQuery, our guide has got you covered! 🎉

If you enjoyed this article and found it helpful, don't forget to share it with your fellow tech enthusiasts. Feel free to leave your thoughts and any alternative methods in the comments section below! 👇

Keep coding and keep learning! 💻💡


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello