This is a migrated thread and some comments may be shown as answers.

How to get the selected menu item text from JavaScript

2 Answers 661 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 11 Oct 2013, 05:42 PM
I would like to get the text for a selected menu item from JavaScript/jQuery in a clean way. I would rather not use jQuery matching for specific classes if possible, I'm hoping for something like:

$("#myMenu").getSelectedItemText()

Alternatively, I guess I could populate a variable during a Select event handler or something. 

Any suggestions?  Thanks!

2 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 14 Oct 2013, 07:11 AM
Hi Ian,

 You can use the following code to get the selected item's text:

function onSelect(e) {
      var item = e.item;
      var text = $(e.item).children(".k-link").text();
}

A live demo is available here: http://demos.kendoui.com/web/menu/events.html

Regards,
Atanas Korchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Ian
Top achievements
Rank 1
answered on 14 Oct 2013, 10:15 AM
Thanks for the reply. I see how this would work and similarly I could also get the selected text outside of a select handler using ".children(".k-state-selected").text()". I was not sure it was good practice, however,  to use specific classes for this purpose (implementation detail) rather than to use a Kendo API function. I'm guessing there is no such function :)
Tags
Menu
Asked by
Ian
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Ian
Top achievements
Rank 1
Share this question
or