"Retrieve the values of the items that have been chosen from a list using jQuery

In my PHP page, I have a list displayed using the following script:

<div id='demo' style="padding-left:20px">

<select name='lists[]' multiple='multiple' class='multiselect' >
<?php

$sql="select list_id,list_name from tbl_list";
$result=mysql_query($sql);
for($i=1;$i<=mysql_num_rows($result);$i++)
{
$row=mysql_fetch_array($result);
?>
<option  value='<?php echo $row['list_id']; ?>' title='<?php echo $row['list_name']; ?>'><?php echo $row['list_name']; ?></option>
 <?php
 }
 ?>           
  </select>
        </form>
      </div>

Now, I want to use jQuery to fetch the values of the selected rows from the list box after selecting multiple items.

Thank you!

Answer №1

$('select').val();

This function will provide the values x, y, z that you have selected.

Answer №2

$('select').val();

To retrieve the value of the select element at that particular moment.

$('select').change(function(){
    alert($(this).val());
});

To continuously capture the updated value everytime it changes.

Here's an example demonstrating multiple selects on a page: http://jsfiddle.net/Mxtey/

I trust this information proves beneficial.

Answer №3

If you want to get exactly what you need, try using $("select").serialize(); But make sure to include a "form" before that and then proceed with

$("form").serialize();

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

Attempting to categorize words containing 4 or more characters alongside words containing 3 or fewer characters using preg_match_all

Currently, I am attempting to categorize words with 4 or more characters together with those containing 3 or less characters using preg_match_all() in PHP. This is specifically for a keyword search feature where users can input phrases like "An elephant," ...

When using vue.js, I am able to successfully copy data by clicking on a text box, but unfortunately

I am facing an issue where the copied value from one text box to another is not passing validation upon clicking a checkbox. I need help resolving this issue, and I also have another concern about validating specific text boxes for numbers using regex with ...

The admin-ajax.php file in Wordpress is sending back php code instead of the expected json format

How do I configure WordPress to return JSON on Nginx? When making Ajax calls in my WordPress site, instead of receiving plain JSON, I see <?php JSON. Everything works fine on the site, except for admin-ajax calls. Example of a regular call: https://i. ...

Can one manipulate the simulation to make isTrusted=true a reality?

Is there a way to simulate an isTrusted=true in touchStart event triggering? Are there any libraries or workarounds that can make this achievable? When I run the touchStart event programmatically versus physically triggering it, the output differs. Below ...

Send function arguments to populate dropdown menus with JSON data

Is there a way to pass the parameter into "foodName" from the function that sends the url and id, without explicitly mentioning it in the function? For example, could you create a more generic function like: call('/url/to/foodlist', "foodLis ...

How to Convert JSON from an Object to a String using jQuery?

Currently, I am utilizing the Ajax Form jQuery plugin to fetch JSON data from a server: /** * A convenient function for the jQuery AJAX form plugin. */ function bindOnSuccess(form, callback) { form.ajaxForm({ dataType: 'json', ...

Incorporating Social Share Buttons to Enhance User Engagement on Your WordPress E

Looking to enhance my product page by adding social icons right below the 'Add to Cart' button. The product page URL is https://flowersforeveryone.co.za/product/cheerful-orange-tulips/. I already have a 'social menu' set up. How can I ...

Lose yourself in the horizontal beauty of the CSS menu

Currently, I am working on an application using ASP.Net MVC4, jquery 1.9, and CSS 2.1. However, I have encountered an issue with the horizontal menu display. Whenever a form with a jquery component is shown, some buttons seem to disappear behind the menu, ...

Accessing a child element within a div with the help of the keyword "this" in jQuery

I am encountering an issue with accessing and updating attributes of iframe elements within HTML containers on a page. When I attempt to loop through each "iframeHolder" and target the iframe element within it, I encounter errors. Below the HTML code snip ...

What steps can I take to direct mobile visitors to the mobile-friendly version of my website?

Looking for a way to automatically redirect users on mobile devices from www.domain.com to the new mobile version at m.domain.com? ...

Can you embed a VueJs Component using jquery?

I have a complex application where I previously utilized jQuery to dynamically change views: $(function(){ $(".changeview").bind("click",function(){ $.post(linkAction,phpData,function(data){ $('#loa ...

What is the reason behind the validity of this statement in PHP?

Can someone please explain to me why the PHP statement `(0x0F | 0xF0)` results in 'whaaa?' being echoed, shouldn't it be `0xFF` instead? If ((0x0FFFFFFF | 0xF0FFFFFF) != 0xFFFFFFFF) { echo 'whaaa?'; } ...

Strategies for thorough module testing in my PHP application using PHPUnit and Zend Framework 2

I have successfully tested my application module and another module separately. However, I would like to run all tests (both for the application module and the other module) together in order to generate a clover report for Jenkins. What steps should I tak ...

Issue encountered when sending information to asmx web service via ajax and displaying the result on an HTML page with a javascript function

I have developed an ASMX web service that looks like this: [ScriptService] public class CurrencyData : System.Web.Services.WebService { [WebMethod] public string DisplayCurrency(double amount, string sign ,string style) { swi ...

Some files are causing PHP file_exists() to return false

I've encountered a strange issue with the file_exists() PHP function on a Linux server. I'm facing a dilemma where file_exists fails to check if an image exists on the server before editing it, even though the absolute file paths are correct. I& ...

What is the process for changing the selected value after it has been clicked and removing the previous selection

Is there a way to change the class of a li element in a PHP view after it has been clicked? <ul> <li> <a href="<?=base_url()?>home" id="home" onclick="select_class('home');" class="shortcut-dashboard" title="Home">Home& ...

Conceal and unveil stationary navigation when scrolling back to the very top of the screen

My current setup includes a navigation that dynamically hides as the user scrolls down and reappears when scrolling up. Additionally, I have applied the class .fixed on #top-wrapper to alter the layout/styling of this specific div. However, I am facing dif ...

Using JavaScript to call a PHP file as the source file

I'm curious about the scenario where a .php file is called as a javascript. What does this signify and in what situations would it be necessary to use such an approach? Example: <head> <script src="dir/myphpfile.php" type="text/javascript" ...

problem updating data with collapsible panels

I am encountering an issue with my accordion control (using jQuery) on the page. It seems that when I insert the contents of my page into an updatepanel, the accordion stops working. However, if I remove the updatepanel, it starts working again. I'm ...

Creating unique CSS class and ID names dynamically in PHP

I have two divs with randomly generated IDs and I would like to use those IDs in my CSS instead of using inline styles. Is there a way to achieve this without creating a separate PHP file with a CSS header? For example, I want the padding for the ID $id_ ...