locked
Get Text Of Selected Dropdown RRS feed

  • Question

  • User1928065626 posted

    I have a dialog that contains a dropdown, please see below.

    <div id="dialog">
       <select id ="choices" ></select>
    </div>
    

    I was able to dynamically popuplate the dropdown in the dialog and was able to get the text of some of the selected items using the jQuery code below.

     $('#choices').change(function () {
       $('#choices option:selected').text();
    });

    The problem with the jQuery above is that the first item of the dropdown is not selected when the dialog opens. This is a problem if I want the text of the first item.

    How do I modify the code above to get the text of any selected item? Thanks in advance.

    Saturday, September 24, 2016 1:13 AM

Answers

  • User347430248 posted

    Hi Madjester,

    use something like below.

    var value = $('#dropDownId:selected').text()

    example

    $(document).ready(function(){ 
      $('#button1').click(function(){ 
        alert($('#combo :selected').text());
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select id="combo">
      <option value="1">Test 1</option>
      <option value="2">Test 2</option>
    </select>
    <input id="button1" type="button" value="Click!" />
    

    Regards

    Deepak

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 24, 2016 9:39 AM

All replies

  • User2117486576 posted

    I was able to dynamically popuplate the dropdown in the dialog
    \

    If you are populating the drop down then add the "selected" attribute to the first option.

    Saturday, September 24, 2016 1:29 AM
  • User347430248 posted

    Hi Madjester,

    use something like below.

    var value = $('#dropDownId:selected').text()

    example

    $(document).ready(function(){ 
      $('#button1').click(function(){ 
        alert($('#combo :selected').text());
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select id="combo">
      <option value="1">Test 1</option>
      <option value="2">Test 2</option>
    </select>
    <input id="button1" type="button" value="Click!" />
    

    Regards

    Deepak

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 24, 2016 9:39 AM