jquery - Set selected item in dropdown-menu -


i need set selected item of dropdown-menu in jquery can't make work.

my dropdown code looks follows:

<button type="button" id="aktiv" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">   aktiv <span class="caret"></span> </button> <ul class="dropdown-menu" role="menu">  <li><a href="#">aktiv</a></li>  <li><a href="#">inaktiv</a></li> </ul> 

the following function can used set text of menu. sets value of button element can retrieve using $('#aktiv').val().

function setaktivmenu(text) {     $('#aktiv').val(text).html(function(i, html) {         return text + html.slice(html.indexof(' <'));     }); } 

you can call function set menu text this:

setaktivmenu('inaktiv'); 

you can set event handler menu options cause menu text change when clicked:

$('#aktiv + .dropdown-menu').on('click', 'li>a', function() {     setaktivmenu($(this).text()); }); 

jsfiddle


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -