Getting data- attributes after onclick

3 posts, 1 answers
  1. Anton Swanevelder
    Anton Swanevelder avatar
    28 posts
    Member since:
    Aug 2008

    Posted 04 Dec 2012 Link to this post

    Hi,

    I fear there might be a really easy answer to this but I just can't figure it out.

    I want to retrieve a data- attribute from a li after the onclick has fired. How do I do this?

    <ul id="searchChoices" class="radioList" >
        <li class="selected" data-bind="click: keyTypeChanged" data-value="0">Option 1</li>
        <li data-bind="click: keyTypeChanged" data-value="1">Option 2</li>
        <li data-bind="click: keyTypeChanged" data-value="2">Option 3</li>
        <li data-bind="click: keyTypeChanged" data-value="3">Option 4</li>
    </ul>
    var searchViewModel = kendo.observable({
     
    keyTypeChanged: function (e) {
            //debugger;
            alert($(this).attr("data-value"));
        }
      
    });
    Thanks in advance,
    Anton
  2. Anton Swanevelder
    Anton Swanevelder avatar
    28 posts
    Member since:
    Aug 2008

    Posted 04 Dec 2012 Link to this post

    Catching up on my jQuery skills...

    I changed the code to be as follows and I now manage to get it. Is there any suggestions to a better KendoUI way of doing this?

    keyTypeChanged: function (e) {
            var id = $(e.target).data().value;
            alert(id);
        }
  3. Kendo UI is VS 2017 Ready
  4. Answer
    Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 05 Dec 2012 Link to this post

    Hello Anton,

    There is no special KendoUI way of getting data from the data attributes. The best way to do this is through the jQuery data method like in your example. Actually we use the same approach (example).

    Regards,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top