Reputation: 459
I want to change Bootstrap dropdown item select with jQuery? I'm newbie in jQuery. I put dropdown list data from database with foreach loop. and I've a button. that have "id" and "value". When I click on that button, i want to change my bootstrap dropdown 'li' selected item to it button's "vaule" and "id".Please help me.
<div class="dropdown" align="right">
<input type="button" id="select_staff" class="btn btn-default btn-sm dropdown-toggle" type="button" data-toggle="dropdown" value="<?php echo $cur_admin_name ?> ▼">
<ul id="admin_cal_list" class="dropdown-menu dropdown-menu-right">
<input type="hidden" id="admin_id" class="form-control">
<?php foreach ($admin_name as $admin_list): ?>
<li id="<?php echo $admin_list["interview_plan_staff_id"]; ?>"><a href="#"><?php echo $admin_list["interview_plan_staff_name"]; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
Upvotes: 1
Views: 11197
Reputation: 19
I'm not sure I 100% understand what you are trying to do. For clarity, you want the li to change to the button's id and value? If so try this:
$(".dropdown-menu li a").click(function(){
document.getElementById("admin_cal_list").innerHTML = $(".dropdown-toggle").attr("id") + " " + $(".dropdown-toggle").attr("value");
});
Demo: http://www.bootply.com/UphW5kYtwE
Upvotes: 0
Reputation: 362270
Like this..
$(".dropdown-menu li a").click(function(){
var selText = $(this).text();
console.log( selText);
$(this).parents('.dropdown').find('.dropdown-toggle').val(selText);
});
Demo: http://www.bootply.com/EuFKxaQ2bO
Upvotes: 2