My goal is to add jQuery to a project that will check if an element has data-attributeno value. For example, with a tag, videoyou can simply add autoplaywithout a value, and it will automatically start. I try to do the same and wonder if this is possible. Here is what I tried, but now it returns false:
$(function() {
$('div').click(function() {
if ($(this).attr('data-specs')) {
console.log('has specs');
} else {
console.log('no specs');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-specs>Specs</div>
Run codeHide result
APAD1 source
share