How can I find out if an data- attribute is set to an empty value?

Stephan Wagner

Is there a way to find out if an data- attribute is set to an empty value or if the value of 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

Scimonster

This is impossible. The two following elements are normalized to the same thing (the first).

<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>

I tested this by checking the outerHTML of both elements. You're going to need to come up with a different way to achieve this.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How can I find the string value of a property given it's value

来自分类Dev

How can I find out how much physical space a record (BLOB) in a sqlite database uses?

来自分类Dev

How can I find out what version is passed to setup in setup.py?

来自分类Dev

How can I set plain value (no json) for cookie in Yii2

来自分类Dev

Can I find out if a given ext4 block is in the inode table, and if so, can I pick it out of a journal with no header by hand?

来自分类Dev

How do I find out the numeric ID of the current Postgres Transaction?

来自分类Dev

How to get html data-attribute value in css regular expressions

来自分类Dev

PhantomJS; If I set a variable inside page.evaluate(), how can I access the value of that variable outside of page.evaluate()

来自分类Dev

How can I efficiently find the accuracy of a classifier

来自分类Dev

How can I set the jenkins authentication token?

来自分类Dev

How can I select the item that doesn't have attribute

来自分类Dev

In Puppet, how can I access a variable/attribute inside a defined type?

来自分类Dev

How can I associate xml text with a preceding empty element in Python?

来自分类Dev

How can i replace an objectAtIndex with value for key?

来自分类Dev

how do I find the closest value to a given number in an array?

来自分类Dev

How can I find who pushed a tag(s) to BitBucket

来自分类Dev

How can I recursively find the *directories* containing a file of a certain type?

来自分类Dev

How can I find most frequent combinations of numbers in a list?

来自分类Dev

How can I use find() on a map inside a vector of maps?

来自分类Dev

How can I find executables installed by NuGet packages?

来自分类Dev

How can I find my browser web log file?

来自分类Dev

How do I find the path of a data file in Dancer to use in Javascript?

来自分类Dev

How can I override the set axios override defaults on specific requests?

来自分类Dev

How can I set environment variable for runComment() in Rhino?

来自分类Dev

How can I set element for entire width of the page? (Android)

来自分类Dev

Windows: How can I use the command line to set registry permissions

来自分类Dev

Windows: How can I use the command line to set registry permissions

来自分类Dev

Find out if I'm on the unity thread

来自分类Dev

How do I find out which process is using so much of my RAM?

Related 相关文章

  1. 1

    How can I find the string value of a property given it's value

  2. 2

    How can I find out how much physical space a record (BLOB) in a sqlite database uses?

  3. 3

    How can I find out what version is passed to setup in setup.py?

  4. 4

    How can I set plain value (no json) for cookie in Yii2

  5. 5

    Can I find out if a given ext4 block is in the inode table, and if so, can I pick it out of a journal with no header by hand?

  6. 6

    How do I find out the numeric ID of the current Postgres Transaction?

  7. 7

    How to get html data-attribute value in css regular expressions

  8. 8

    PhantomJS; If I set a variable inside page.evaluate(), how can I access the value of that variable outside of page.evaluate()

  9. 9

    How can I efficiently find the accuracy of a classifier

  10. 10

    How can I set the jenkins authentication token?

  11. 11

    How can I select the item that doesn't have attribute

  12. 12

    In Puppet, how can I access a variable/attribute inside a defined type?

  13. 13

    How can I associate xml text with a preceding empty element in Python?

  14. 14

    How can i replace an objectAtIndex with value for key?

  15. 15

    how do I find the closest value to a given number in an array?

  16. 16

    How can I find who pushed a tag(s) to BitBucket

  17. 17

    How can I recursively find the *directories* containing a file of a certain type?

  18. 18

    How can I find most frequent combinations of numbers in a list?

  19. 19

    How can I use find() on a map inside a vector of maps?

  20. 20

    How can I find executables installed by NuGet packages?

  21. 21

    How can I find my browser web log file?

  22. 22

    How do I find the path of a data file in Dancer to use in Javascript?

  23. 23

    How can I override the set axios override defaults on specific requests?

  24. 24

    How can I set environment variable for runComment() in Rhino?

  25. 25

    How can I set element for entire width of the page? (Android)

  26. 26

    Windows: How can I use the command line to set registry permissions

  27. 27

    Windows: How can I use the command line to set registry permissions

  28. 28

    Find out if I'm on the unity thread

  29. 29

    How do I find out which process is using so much of my RAM?

热门标签

归档