user2823235 user2823235 - 2 months ago 14
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>
</div>


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

Answer

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

 $('#sector_status').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

DEMO: https://jsfiddle.net/qasmsa0j/1/

Comments