0

I use opacity: 0 for my class .best-match.

The CSS (Sass) looks like this:

.best-match
  opacity: 0

This is what I see in my browser:

.best-match[_ngcontent-tli-c4] {
  opacity: 0;
}

I would like to check with JavaScript if the property is set to 0. I have tried it with

if (document.querySelector('.best-match').style.opacity === '0') { ... }

but the output is always false. I have also tried it with @ViewChild():

if (this.bestMatchContainer.nativeElement.style.opacity === '0') { ... }

but that also returns false. What's wrong with my code?

0

2 Answers 2

1

The style property only works for inline styles (the value of the style attribute on that element) on that element. If you want to look at all the styles that are effecting the element (including styling from css) look at this. This might give you something that looks like this:

if (window.getComputedStyle(document.querySelector('.best-match')).opacity === 0) {...
Sign up to request clarification or add additional context in comments.

Comments

1

Try to use Window.getComputedStyle() function.

const element = document.querySelector('.best-match);

if (window.getComputedStyle(element).getPropertyValue('opacity') === 0) {
    // try something
}

Official guide: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.