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

Getting data- attributes after onclick

2 Answers 764 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Anton Swanevelder
Top achievements
Rank 2
Anton Swanevelder asked on 04 Dec 2012, 07:27 AM
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 Answers, 1 is accepted

Sort by
0
Anton Swanevelder
Top achievements
Rank 2
answered on 04 Dec 2012, 08:50 AM
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);
    }
0
Accepted
Alexander Valchev
Telerik team
answered on 05 Dec 2012, 02:11 PM
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!
Tags
MVVM
Asked by
Anton Swanevelder
Top achievements
Rank 2
Answers by
Anton Swanevelder
Top achievements
Rank 2
Alexander Valchev
Telerik team
Share this question
or