JavaScript setting pointer-events

JavascriptCss

Javascript Problem Overview


I am trying to set pointer-events: none for a div through JavaScript, however the property is not being applied.

I have tried:

document.getElementById("div").setAttribute("pointer-events", "none");

And:

document.getElementById("div").style.pointer-events = "none";

But neither worked, any ideas?

Javascript Solutions


Solution 1 - Javascript

Your first attempt failed because it was a CSS property and not an attribute (part of the style attribute).

Your second attempt failed because for some off reason, when making this API casing-like-this gets converted to camelCasingLikeThis . This is likely because the folks who built JavaScript and the folks who built CSS weren't very well coordinated.

The following should work:

document.getElementById("div").style.pointerEvents = "none";

Solution 2 - Javascript

You can access style properties via Bracket notation like this:

document.getElementById("div").style['pointer-events'] = 'auto';

No need in camel case modification in this case.

Solution 3 - Javascript

There is an example in a book which uses element.setAttributeNS(null, "pointer-events", "none"); instead of using element.style.pointer-events = "none";.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1334130View Question on Stackoverflow
Solution 1 - JavascriptBenjamin GruenbaumView Answer on Stackoverflow
Solution 2 - JavascriptalehaView Answer on Stackoverflow
Solution 3 - JavascriptBehnoodView Answer on Stackoverflow