Get selected text in a combo box using jQuery by the name of the combo box?

How to get selected text value from combo box using jQuery.

I have only the "name" of the combined field.

So, I want the text of the selected item using the name of the combo box, not the identifier.

I have,

var selected_fld = ( $(this).attr('name') );

How can I continue?

+5
source share
5 answers
$('select[name=nameOfTheBox]').val();

or

$('select[name=nameOfTheBox] option:selected').val();

will give you the value of the selected option

$('select[name=nameOfTheBox] option:selected').text();

will give you text

+11
source

This can be done simply with the following to get the actual text value ...

var value = $("[name='MyName'] option:selected").text();

or this to get the value attribute ...

var value = $("[name='MyName']").val();

html "MyText", "MyValue"

<select name="MyName">
   <option value="MyValue" selected="selected">MyText</option>
</select>

+7

<select name="name1">
      <option value="val1">val1</option>
      <option value = "val2">val2</option>
</select>

var text = $("select[name='name1'] option:selected").text();
+1
<select id ="myCombo">
   <option value="Play selected="selected">Here</option>
   <option value="Once">Again</option>
</select>

$('#myCombo option:selected').text();

It will bring you back here

$('myCombo option:selected').val();

This will return you "Play"

0
source
    <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-3.1.0.js"></script>
    <script>
        $(function () {
            $('#selectnumber').change(function(){
                alert('.val() = ' + $('#selectnumber').val() + '  AND  html() = ' + $('#selectnumber option:selected').html() + '  AND .text() = ' + $('#selectnumber option:selected').text());
            })
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <select id="selectnumber">
            <option value="1">one</option>
            <option value="2">two</option>
            <option value="3">three</option>
            <option value="4">four</option>
        </select>

    </div>
    </form>
</body>
</html>

Click to view OutPut screen

Thank...:)

0
source

All Articles