How can I detect with jQuery / JavaScript, if a given data-*
Attribute of a certain DOM element has an empty string as a value? Common comparison operators do not seem to work properly, as the example below shows. I
May be close to: Checking for HTML5 data attribute with no value or set data- html5 value by default
But none of these answers matched this issue.
The HTML
<ul class="list">
<li class="list--item" data-id="x6gftcc-44">First entry</li>
<li class="list--item" data-id="">Last entry</li>
</ul>
<div class="message"></div>
Some CSS
.list{
list-style-type: none;
}
.list--item{
display: inline-block;
min-width: 200px;
padding: 20px;
border: 1px solid rgb(60,64,73);
cursor: pointer;
}
.message{
display: none;
}
.message.active{
display: block;
}
.true{
color: green;
}
.false{
color: red;
}
The JavaScript
$(document).ready(function(){
$('.list--item').click(function(event, target){
function hasId(){
if ($(this).data('id') != ''){
return true;
}
else{
return false;
}
};
var entry = {
id: $(this).data('id'),
hasId: hasId()
};
if (entry.hasId){
$('.message').addClass('active true');
$('.message').text('Picked entry ID: ' + entry.id);
}
else{
$('.message').addClass('active false');
$('.message').text('Entry ID not available');
}
})
});
Example available at CodePen.
The attributes width and height of <img> elements expect a non-negative integer, so an empty string is not allowed. Either define the correct dimension, or remove this attribute. Learn more: HTML Spec: images.
Use the hasAttribute() method to check if a data attribute exists, e.g. if (el. hasAttribute('data-example')) {} . The hasAttribute method returns true if the provided attribute exists, otherwise false is returned. Here is the HTML for the examples in this article.
We can either use the dataset property to get access to the data attributes or use the . getAttribute() method to select them by specifically typing their names.
As is common with all attributes, in the application/xhtml+xml serialisation, XML rules apply and the attribute must have an explicit name and (quoted) value. Just the attribute name. The value is implicitly the empty string.
Your code needs a little tweak in order to work:
$(document).ready(function(){
$('.list--item').click(function(event, target){
function hasId(my){
if (my.data('id') != ''){
return true;
}
else{
return false;
}
};
var entry = {
id: $(this).data('id'),
hasId: hasId($(this))
};
if (entry.hasId){
$('.message').addClass('active true');
$('.message').text('Picked entry ID: ' + entry.id);
}
else{
$('.message').addClass('active false');
$('.message').text('Entry ID not available');
}
});
});
Basically was just a mather of scope; inside your
hasId
function you are referencing to$(this)
and asking for thedata('id')
attribute, but in that scope$(this)
isn't referencing the initialentry
object.
The Problem is, inside your hasId()
function $(this)
is not referring to the right element. Try this:
function hasId(element){
if (element.data('id') != ''){
return true;
}
else{
return false;
}
};
var entry = {
id: $(this).data('id'),
hasId: hasId($(this))
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With