atefeh
atefeh

Reputation: 31

Get text and value of dropdown list using jquery

I have 4 dropdownlist in my view in asp.net mvc 5 and I want to get text and value of each select tag to send to action I use below code but every time I get the text and value of the first dropdownlist

<select id="se1" class="mySelect" onchange="f22()">
        <option value="@item.ID">1</option>
        <option value="@item.ID">2</option>
        <option value="@item.ID">3</option>
        <option value="@item.ID">4</option>
        <option value="@item.ID">5</option>
</select>
<script src="~/Scripts/jquery-1.9.1.js"></script>

<script>
    function f22() {
        var a = $("#se1 option:selected").text();
        var b = $("#se1 option:selected").val();
        alert(a)
        alert(b)
    }
</script>

Upvotes: 1

Views: 544

Answers (1)

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

Pass the element to js function using this as the parameter of f22 function and then use this in js like following. Hope this will help you.

<select id="se1" class="mySelect" onchange="f22(this)">
    .........
    .........
</select>

function f22(elm) {
    var a = $(elm).find("option:selected").text();
    var b = $(elm).val();
    alert(a)
    alert(b)
}

Upvotes: 1

Related Questions