Skip to content

Return null from .attr when attributes don't exist #2118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gibson042 opened this issue Mar 5, 2015 · 8 comments
Closed

Return null from .attr when attributes don't exist #2118

gibson042 opened this issue Mar 5, 2015 · 8 comments

Comments

@gibson042
Copy link
Member

.attr("nonexistent") currently returns undefined, probably from the days before .prop. But the native DOM getAttribute and Sizzle.attr both return null in such cases. Also, .attr( name, null ) removes attributes, and it would be very convenient to allow universal round-tripping.

Let's consider changing the return value to accommodate.

Note that setter treatment of undefined is explicitly out-of-scope for this ticket, as are .prop and .data round-tripping, although those are certainly related topics.

@timmywil
Copy link
Member

timmywil commented Mar 9, 2015

@dmethvin We're leaning towards doing this for beta. Do you know of a use case that would make this catastrophic?

@dmethvin
Copy link
Member

dmethvin commented Mar 9, 2015

It seems like returning null is a good idea, although who knows what it will break in the wild. The only way to find out is to change it! We should continue to return undefined for empty jQuery collections tho.

@gibson042
Copy link
Member Author

We should continue to return undefined for empty jQuery collections tho.

Thanks; that reminded me to create #2134.

timmywil pushed a commit that referenced this issue Mar 16, 2015
Fixes gh-2118
Close gh-2129

Conflicts:
	test/unit/attributes.js
@scottgonzalez
Copy link
Member

It seems like returning null is a good idea, although who knows what it will break in the wild.

Of course the answer turns out to be jQuery UI :-)

I'm working on updates right now.

@timmywil
Copy link
Member

The team decided to revert this due to breakages in UI and mobile.

@dmethvin
Copy link
Member

We backed this out for 1.12/2.2 which definitely seems right, but it's also excluded from 3.0. Should it be? That is, do we want a non-existent attribute to return null as proposed here? If not I want to remove the milestone and tag.

@mgol mgol removed this from the 3.0.0 milestone Mar 23, 2016
@mgol
Copy link
Member

mgol commented Mar 23, 2016

I removed the milestone due to the revert and opened the issue & added the "Needs review" label to account for the Dave's question.

@mgol mgol reopened this Mar 23, 2016
@markelog
Copy link
Member

We backed this out for 1.12/2.2 which definitely seems right, but it's also excluded from 3.0. Should it be?

I remember this discussion, we reverted cause it might be dangerous to do which proved the UI breakage, where is literally no gain from, except to align with standard, whereas behaviour in DOM spec is very questionable in the first place, since in all others JS API you get undefined value not null value

@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

6 participants