How can I find out if an data- attribute is set to an empty value?
Posted
by
Stephan Wagner
on Stack Overflow
See other posts from Stack Overflow
or by Stephan Wagner
Published on 2014-08-22T15:56:16Z
Indexed on
2014/08/22
16:21 UTC
Read the original article
Hit count: 259
Is there a way to find out if an data- attribute is set to an empty value or if it is not set at all?
See this fiddle example (Check the console when clicking on the elements):
http://jsfiddle.net/StephanWagner/yy8qvwfp/
<div onclick="console.log($(this).attr('data-test'))">undefined</div>
<div data-test="" onclick="console.log($(this).attr('data-test'))">empty</div>
<!-- this one will also return an empty value -->
<div data-test onclick="console.log($(this).attr('data-test'))">null</div>
<div data-test="value" onclick="console.log($(this).attr('data-test'))">value</div>
Im having the issue with the third example. I need to know if the attribute actually is set to an empty value or if it is not set at all. Is that actually possible?
EDIT: The reason I'm asking is that I'm updating content with the attributes value, so data-test="" should update the content to an empty value, but data-test should do nothing at all
© Stack Overflow or respective owner