question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setting data attributes in html not working

See original GitHub issue

Setting a data attribute using attr throws TypeError: Cannot read property 'test' of undefined.

Example

$('ul').attr('data-url', 'http://www.example.com');
Error Stack
TypeError: Cannot read property 'test' of undefined
    at Object.$.each (D:\node\frix\lib\html-creator.js:141:39)
    at exports.each (D:\node\frix\node_modules\cheerio\lib\api\traversing.js:300:24)
    at HTMLCreator.insertContent (D:\node\frix\lib\html-creator.js:131:12)
    at HTMLCreator.resolveElements (D:\node\frix\lib\html-creator.js:56:10)
    at promises.push.access.readElement.then.template (D:\node\frix\lib\html-creator.js:113:23)
    at tryCallOne (D:\node\frix\node_modules\promise\lib\core.js:37:12)
    at D:\node\frix\node_modules\promise\lib\core.js:123:15
    at flush (D:\node\frix\node_modules\asap\raw.js:50:29)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
drsnydercommented, Jun 13, 2018

I was also tripped up by this. According to the documentation, one would think that setting data attributes is possible with data.

The snippet for data() reads:

Method for getting and setting data attributes. Gets or sets the data attribute value for only the first element in the matched set.

So I was surprised when element.data('key', 'value') doesn’t change the value.

1reaction
garyeecommented, Jun 28, 2018

@drsnyder even if it says something else in the documentation, it is standard jquery behavoir, took me some time to figure out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML data attribute not changing [duplicate] - Stack Overflow
When I click on Set Data, it should change all paragraph tags with the data attribute 'tag' to 'Complete' but it's not working....
Read more >
Using data attributes - Learn web development | MDN
Using data attributes. HTML is designed with extensibility in mind for data that should be associated with a particular element but need not...
Read more >
How to Use HTML Data Attributes - DEV Community ‍ ‍
Adding a data attribute is easy. Any HTML element can have any number of data attributes added to its opening tag. We simply...
Read more >
Why data- attribute sometimes doesn't work?
Why it doesn't work consistently? I've checked rendered html page and all of those links have proper data-item populated.
Read more >
HTML data-* Attribute - W3Schools
The attribute name should not contain any uppercase letters, and must be at least one character long after the prefix "data-" · The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found