Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  164] [ 5]  / answers: 1 / hits: 29498  / 10 Years ago, wed, july 23, 2014, 12:00:00

I have a ul which is <ul> list, I would like to get the selected value in different function and the list looks like below



<ul class=dropdown-menu role=menu id=ChooseAreaList>
<li><a href=#>Select Destination</a></li>
<li><a href=#>HSR</a></li>
<li><a href=#>Bommanahalli</a></li>
<li><a href=#>Kormangala</a></li>
</ul>


by applying some CSS,JS I am able to get selected value of list like this...



$(.dropdown-menu li a).click(function(){
var selText = $(this).text();///User selected value...****
$(this).parents('.btn-group').find('.dropdown-toggle').html(selText+' <span class=caret></span>');
});


but if i am trying to get this selected value in different function its not working...like this



function BusinessLogic(){
var selText = $('.dropdown-menu li a').text();
alert(Its displaying Entire List+selText);
}


How to do I get selected value of that list in different function?


More From » jquery

 Answers
15

You can put class=selectedLi in a to indicate the last selected value from dropdown and use same as jquery selector in your BusinessLogic function :



$(.dropdown-menu li a).click(function(){
// remove previously added selectedLi
$('.selectedLi').removeClass('selectedLi');
// add class `selectedLi`
$(this).addClass('selectedLi');
var selText = $(this).text();///User selected value...****
$(this).parents('.btn-group').find('.dropdown-toggle').html(selText+
' <span class=caret></span>');
});


And in below function use selectedLi in jquery selector :



function BusinessLogic()
{
var selText = $('.dropdown-menu li a.selectedLi').text();

alert(Its displaying Entire List);

}

[#70088] Monday, July 21, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayaw

Total Points: 749
Total Questions: 88
Total Answers: 86

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
tayaw questions
;