jquery icon indicating copy to clipboard operation
jquery copied to clipboard

CSS: .css() returns undefined for custom properties on detached elements

Open Kevinjohn opened this issue 4 months ago • 3 comments

Description

Calling .css() to read a CSS custom property from a detached element returns undefined, even when jQuery just set that inline property.

For a detached element in Chrome, getComputedStyle( elem ).getPropertyValue( "--foo" ) returns an empty string and computed[ "--foo" ] is undefined. That leaves ret as undefined, so the current ret === "" detached fallback is skipped. The fallback delegates to jQuery.style(), whose getter also reads style[ name ]; custom properties require getPropertyValue().

Minimal reproduction against current main (7e1efd77):

const elem = jQuery( "<div>" ).css( "--foo", "5" );

console.log( elem[ 0 ].style.getPropertyValue( "--foo" ) ); // "5"
console.log( elem.css( "--foo" ) );                         // undefined

Expected: elem.css( "--foo" ) returns "5", matching attached elements and the value stored in the inline style.

Actual: it returns undefined.

Tested in Chrome 150 on macOS. The existing custom-property tests only exercise attached elements.

Link to test case

The reduced code above reproduces after loading the latest development build. It was also verified against a local build of main at 7e1efd77.

Screenshot

Detached custom property returns undefined before the fix

Kevinjohn avatar Jul 16 '26 16:07 Kevinjohn

After the focused fix, the same detached element returns the inline custom-property value:

Image

Kevinjohn avatar Jul 16 '26 17:07 Kevinjohn

Closing per our contribution policy.

This submission doesn't correspond to a problem encountered in real usage and appears to have been produced by pointing an automated tool at the repository. Per the policy, we close these without review regardless of technical merit. Repeat submissions may result in a ban.

If you hit a genuine problem while using jQuery, please open an issue with a test case describing how you encountered it before submitting code.

timmywil avatar Jul 20 '26 16:07 timmywil

I hadn't clocked that @timmywil - apologies!

Was looking for a way to contribute in my week off, appreciate all of my PRs will be edge cases.

I've noted, and taken on board, your comments :)

Kevinjohn avatar Jul 20 '26 17:07 Kevinjohn