I am trying the get an <li>
element by data attribute value (data-event_id=1282).
<div id="timeline">
<ul>
<li class="timeline-event">
<div>Event Body</div>
</li>
</ul>
</div>
$el = $('#timeline ul').find("li.timeline-event").data('event_id');
console.log($el); // returns 1282
$el = $('#timeline ul').find("li.timeline-event[data-event_id='1282']").html();
console.log($el); // returns undefined
What am I doing wrong?
EDIT - Solution
The data is set using .data('event_id', '1282') elsewhere in the code.
jQuery is unable to get the data if it is set using .data().
I found the solution here using the filterByData function by @psycho brm. It works!
jQuery how to find an element based on a data-attribute value?
$.fn.filterByData = function(prop, val) {
return this.filter(
function() { return $(this).data(prop)==val; }
);
}
I presume this must be a jQuery bug though?