Tips for retrieving the selected option from a dropdown menu

I have a dropdown menu with checkboxes that can be selected using SumoSelect. Here is the markup for the dropdown menu:

<select multiple="multiple" name="somename" id="uq" class="select">
    <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
   <option value="mercedes">Mercedes</option>
   <option value="audi">Audi</option>
   <option value="bmw">BMW</option>
</select>

And here is the jQuery function to handle the dropdown click event...

$(document).ready(function () {

        $('.select').SumoSelect({});
        var v = $("#uq").val();

    });

I am referencing this link to implement the multiselect checkbox option in the dropdown menu, but I am struggling to retrieve the selected values.

Your assistance would be greatly appreciated. Thank you.

Update

$("#submit").click(function (event) {
    event.preventDefault();
    alert(v);
    console.log(v);
});

Update 2

$(document).ready(function () {

       $('.select').on('change', function (e) {
           console.log($(this).val()) // value
       }).SumoSelect({});

       $("#submit").click(function (evt) {
           evt.preventDefault();

           var v = $("#uq").val();
           alert(v)

Answer №1

To capture the value upon change event, you can bind to the change event and retrieve the value using the following method

$(document).ready(function () {

    $('.select').on('change', function(e) {
        console.log($(this).val()) // output value
    }).SumoSelect({})
});

Check out this JSFiddle Link - go to the bottom for .ready code sample

Answer №2

To access the values that have been selected, you can do so right after the choices are made. For example:

$('#read-btn').click(function(){
  var value = $("#uq").val();
  alert(value)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple="multiple" name="somename" id="uq" class="select">
    <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
   <option value="mercedes">Mercedes</option>
   <option value="audi">Audi</option>
   <option value="bmw">BMW</option>
</select>
<button id="read-btn">read all choices</button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm curious as to why these two divs are positioned side by side. Can anyone shed some light on why this footer isn't functioning properly

My goal is to showcase the logo along with 3 buttons containing all the social media links underneath it. However, for some reason, my 2 divs are displaying inline instead of flex or block. Can anyone shed light on why this isn't functioning as expect ...

The functionality of `config.assets.debug = true` fails to

I've encountered an issue with the configuration on development where config.assets.debug = true doesn't seem to work correctly. Instead of having multiple separate JavaScript and CSS file inclusions, I'm getting a consolidated one: <li ...

Axis Labels for x and y in Flot Chart

Have you ever wondered if it's possible to display titles for the x and y axes on a Flot graph? Take a look at the following code snippet: $.plot($("#placeholder_1w"), [d], { series: { lines: { show: true, fill: false, fillColor: "red" } ...

The div is not positioned on the left side

I'm struggling with aligning three divs next to each other in HTML and CSS. I've set them to float: left, but they are not obeying this style rule. .threethings { width: 20%; } .threethings div { text-align: center; position: relative; ...

Divs with floating left have a complete vertical border on their right side

I would like the first and second divs to have a full height border on their right side while floating left. CSS: div.wrapper { border: 1px solid black; } div.wrapper > div { text-align: center; width: 50px; padding-left: 5px; fl ...

What is the best way to monitor the quantity of server-side requests being made while utilizing jQuery Validate's remote feature?

Currently, I am implementing jQuery Validate to handle form validation on my website. In this process, I'm utilizing an Ajax call to verify certain data and I find the need to keep track of the number of attempts or calls made to the server. For inst ...

Having trouble passing a jQuery variable containing a string value to PHP through the jQuery AJAX function?

Below is the jQuery function code snippet: function processMessage() { if (textValue != "") { messageString='<div class="alert-box round"><p class="text-left">' + username + ':' + textValue + '</p>< ...

The right column in a relatively positioned layout descends as an element in the left column is revealed

Currently in the process of constructing a registration form for our website, everything is going smoothly with one minor hiccup. The form consists of two columns where users enter their information. Initially, when designing elements for the first (left) ...

Confusion surrounding asynchronous functions in Node.js

When handling routes or endpoints with multiple operations, I often encounter scenarios where I need to perform additional actions. For instance, when deleting an item, it's necessary to also remove the related file from S3 along with deleting the col ...

Populate a dropdown list with array elements using Javascript in ReactJS

I am encountering an issue while trying to populate a dropdown with data from the 'options' array. The error message states that it cannot read property appendChild of null. Using 'document.getElementByClassName' instead of document.ge ...

Using jQuery to retrieve the content of a textarea and display it

I need help finding the right way to read and write to a Linux text file using JavaScript, jQuery, and PHP. Specifically, I want to retrieve the value from a textarea (#taFile) with jQuery ($("#taFile").val();) and send it via $.post to a PHP script that w ...

Issue: The grid is not clearing the last record during search

I am having an issue with the search button and grid load functions. The first click of the search button works fine, but on the second click, it does not clear the old records. Can someone please help me figure out where I am going wrong? SpecialtySear ...

Error encountered when transitioning to TypeScript: Unable to resolve '@/styles/globals.css'

While experimenting with the boilerplate template, I encountered an unusual issue when attempting to use TypeScript with the default NextJS configuration. The problem arose when changing the file extension from .js to .tsx / .tsx. Various versions of NextJ ...

Is it advisable to incorporate await within Promise.all?

Currently, I am developing express middleware to conduct two asynchronous calls to the database in order to verify whether a username or email is already being used. The functions return promises without a catch block as I aim to keep the database logic se ...

Tips for styling React Material-UI list items with fontAwesome icons and Tailwind.css

I would like to align the text of my list items to the left. Additionally, I want all the icons to be the same size as the envelope icon used in Gmail list item. Currently, my code looks like this: https://i.stack.imgur.com/9LNOs.png How can I achieve th ...

Toggle visibility of div based on current business hours, incorporating UTC time

UPDATE I have included a working JSFiddle link, although it appears to not be functioning correctly. https://jsfiddle.net/bill9000/yadk6sja/2/ original question: The code I currently have is designed to show/hide a div based on business hours. Is there a ...

Next.js allows for passing dynamically loaded server-side data to all components for easy access

(I've recently started working with Next.js and inherited a project built using it, so please forgive me if this is something obvious that I'm missing) I have a set of data that needs to be loaded server-side on each request. Initially, I had im ...

Error: The API_URL_KEY variable has not been declared

hardhat.config.js require("@nomicfoundation/hardhat-toolbox"); /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.18", }; /* @type import('hardhat/config').HardhatUserConfig* ...

Loop through the elements of a class in JavaScript and choose all except for the one that was

Imagine having 5 div elements, each with a similar onclick-function that hides the other divs when clicked. HTML: <div id="1" class="divs" onclick="hide()"></div> <div id="2" class="divs" onclick="hide()"></div> <div id="3" cla ...

Submit field values only once the form has been confirmed to be validated

My current project involves using AJAX to submit form values, with the added requirement of implementing form validation. If any field in the form is empty, the submission should be prevented. I'm facing a challenge in figuring out how to halt the for ...