jquery / jquery/api.jquery.com

Specify that .data and .attr can't be mixed

Open
#1,143 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
325
Forks
260
PR merge metrics
No merged PRs in 30d

Description

I found out the hard way that you can't mix usage of .data and .attr.

The .data documentation clearly states that it doesn't change the DOM upon setting a value.

However, the .attr documentation talks at length about the difference between it and prop, but does not state that it doesn't change the dataset of the DOM node, as demonstrated here: https://codepen.io/cosmo0/pen/YzKVBPp?editors=0011

So your code has to be very strict about using either .data or .attr, because mixing both can result in unexpected behavior, at least when you don't know how jQuery works.

I understand it's by-design, but maybe a warning in the .attr documentation (and/or the .data?) would help people to not fall for this trap?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the .attr and .data documentation and review the linked CodePen example to confirm the behavior described. Done means the relevant documentation clearly warns that mixing .data and .attr can produce unexpected results because they do not update the same DOM state.

Written by the indexing model from the issue text.

Assessment

Tech stack
jquery
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.