Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the selected value and key from an html dropdown list in javascript on php

 <select id="facetList" style="width:120px;" class="text ui-widget-content ui-corner-all">
        <?php
        foreach($claAddArray as $k => $v)
        {
           echo "<option value=\"$k\">$v</option>";
        }
        ?>   
        </select>




$("#btnSubmit").click(function() {

    var $fId = ?????????????????


    });
like image 728
Ahmad Farid Avatar asked Dec 22 '10 12:12

Ahmad Farid


People also ask

How do I get the selected value from a dropdown list using JavaScript?

The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.

How do you get the selected value of dropdown in php with submit?

PHP 8 Get Single Selected Values of Select Box php if(isset($_POST['submit'])){ if(! empty($_POST['Fruit'])) { $selected = $_POST['Fruit']; echo 'You have chosen: ' . $selected; } else { echo 'Please select the value. '; } } ?>


2 Answers

jQuery's .val() will return the currently selected option's value if run on the select list itself. In your case $("#facetList").val();

use $("#facetList option:selected").text(); for the text of the option tag.

like image 152
DampeS8N Avatar answered Oct 28 '22 20:10

DampeS8N


document.getElementById('facetList').options[document.getElementById('facetList').selectedIndex].value
like image 34
seriousdev Avatar answered Oct 28 '22 19:10

seriousdev