user1919 user1919 - 1 year ago 189
jQuery Question

Programmatically set value on bootstrap dropdown menu with jquery

I have a bootstrap dropdown menu with two options:

<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" id="button-methods" type="button" data-toggle="dropdown" style="width: 100%;">"Pick Up Method To Create a Layer"
<span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-methods" style="width: 95%;">
<li><a href="#" id="excel_layer"><span class="glyphicon glyphicon-chevron-right"></span>"Create a layer"</a></li>
<li><a href="#" id="empty_layer"><span class="glyphicon glyphicon-chevron-right"></span>"Create an empty layer"</a></li>

Is there a way to programmatically set the second option of the dropdown menu when the page loads?

Answer Source

You can use JQuery selector to select the second LI DOM object under the appropriate parent as you wish and set it in the

    // Set value for your Dropdown
    $('div ul li').eq(1).find('span').eq(1).text('what ever you need');

    // Direct Selector to the object is also as (Maybe better than the above one I gave you before):
    $('#empty_layer').text('what ever you need');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download