Jquery get only the selected item in Bootstrap

There are bit similar questions like this in which the answers are right. This question is regarding, to get only the selected item not the entire array.

<div class="btn-group">
<a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false" id="change_regions">
    Dropdown
    <span class="caret"></span>
</a>
<ul class="dropdown-menu">
    <li><a href="#">test1</a></li>
    <li><a href="#">test2</a></li>
    <li><a href="#">test3</a></li>
</ul>

If I use something like below I get many attributes which I don't need.

$(".dropdown-menu li a").click(function() {
   $(".btn:first-child").text($(this).text());
   $(".btn:first-child").val($(this).text());
});

Upvotes: 0

Views: 49

Answers (1)

Rikin
Rikin

Reputation: 5473

It work alright, updates text of .btn as expected:

$(".dropdown-menu li a").click(function() { $(".btn:first-child").text($(this).text()); $(".btn:first-child").val($(this).text()); });

https://jsfiddle.net/itsrikin/vseam36j/2/

In your example you dont need to use .val() though, .text() would give you right value.

Upvotes: 1

Related Questions