user2823235 user2823235 - 1 year ago 83
Ajax Question

How to display selected attribute value first in dropdown using jquery?

I am trying to display selected attribute value default in drop-down by using jQuery, attribute is assigning but value is not showing default, below is my code, It always showing Select Status only

<div class="selector full-width">
<select id="sector_status" name="sector_status">
<option value="">-----Select Status-----</option>
<option value="1" selected="selected">Active</option>
<option value="2">Inactive</option>
</select><span class="custom-select">-----Select Status-----</span>

$("#sector_status option[value="+Status+"]").attr("selected","selected");

Answer Source

To display selected value of drop down use change() function

        var selected_val = $('#sector_status :selected').text(); //Get selected text 
        $(".custom-select").text(selected_val); //Replace selected text 

To get the currently selected text use text() function


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download