Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve id value in Kendo menu

I am using Kendo menu in my project. I want to retrieve id value when I click on the selected item. I used onSelect event and I am able to retrieve the selected item Text. How can I retrieve the id value?

like image 366
steve Avatar asked Dec 27 '22 19:12

steve


2 Answers

You can use HTML5 data atrributes to accomplish this.

HTML

<div id="example" class="k-content">
    <ul id="menu">
        <li>
            First Item
            <ul>
                <li data-id="12345">Sub Item 1 with ID</li>
                <li>Sub Item 2</li>
                <li>Sub Item 3</li>
                <li>Sub Item 4</li>
            </ul>
        </li>
        <li>
            Second Item
            <ul>
                <li>Sub Item 1</li>
                <li>Sub Item 2</li>
                <li>Sub Item 3</li>
                <li>Sub Item 4</li>
            </ul>
        </li>
        <li>
            Third Item
            <ul>
                <li>Sub Item 1</li>
                <li>Sub Item 2</li>
                <li>Sub Item 3</li>
                <li>Sub Item 4</li>
            </ul>
        </li>
        <li>
            Fourth Item
            <ul>
                <li>Sub Item 1</li>
                <li>Sub Item 2</li>
                <li>Sub Item 3</li>
                <li>Sub Item 4</li>
            </ul>
        </li>
        <li>
            Fifth Item
            <ul>
                <li>Sub Item 1</li>
                <li>Sub Item 2</li>
                <li>Sub Item 3</li>
                <li>Sub Item 4</li>
            </ul>
        </li>
    </ul>
</div>

And the Javascript:

        <script>
            $(document).ready(function() {


                function onSelect(e) {
                    var id = $(e.item).attr('data-id');
                }

                $("#menu").kendoMenu({
                    select: onSelect               
                });
            });
        </script>
like image 80
BentOnCoding Avatar answered Dec 29 '22 09:12

BentOnCoding


You can set an ID in the UL/LI structure from which you initialize it (check the Robotsushi's answer). However if you want to initialize the menu dynamically you can use something like this - http://jsfiddle.net/MMRCf/8/

like image 45
Bundyo Avatar answered Dec 29 '22 08:12

Bundyo