0

I tried the following code to remove a property from my css but doesn't work.

 document.getElementById("wrap").style.removeProperty('overflow-x');

The CSS:

#wrap{
overflow-x : hidden;
}

When I run the javascript, I get the error Cannot read property 'style' of null

7
  • Duplicate? stackoverflow.com/questions/195951/… Commented Jan 8, 2015 at 1:50
  • @Scriptable i tried that but didn't work Commented Jan 8, 2015 at 1:52
  • What do you mean by "doesn't work"? Describe your problem. Are you getting an error of some sort? If so, post it in your question. Commented Jan 8, 2015 at 1:55
  • You are testing this code with an ipad/iphone right? Commented Jan 8, 2015 at 1:55
  • @DominatorX the id wrap still contain the overflow-x property Commented Jan 8, 2015 at 1:57

1 Answer 1

4

The code posted would remove overflow-x style in the following scenareo:

<div id="wrap" style="overflow-x: hidden"></div>

CSS applied properties are not applied directly to the style attribute of an element - they are applied at a layer higher.

Effectively, there's nothing to remove on the style property, but you can set a value there instead, which would override the value applied from CSS properties (unless css has !important flag specified)

Try this instead:

document.getElementById("wrap").style.overflowX = 'visible';

As your specific scenario is about applying the style based on some browser detection, I suggest the following javascript:

var userAgent = window.navigator.userAgent.toLowerCase();
var is_ios = /iphone|ipod|ipad/.test( userAgent );
if (!is_ios) document.body.className += 'not-ios';

With CSS:

.not-ios #wrap {
    overflow-x : hidden;
}

You could also use class "wrap" instead of id to make this more re-usable (if required)

Sign up to request clarification or add additional context in comments.

8 Comments

I see this worked but how to remove the css? not style.
Well your CSS rule targets elements with ID "wrap". You could change/remove the ID of the element, or change the CSS rule to target a specific classname which you remove for iOS devices.
Then you just remove the class from the element instead. Some possible methods here.
how to select the id then? since it's .wrap.
You could loop through elements found using document.getElementsByClassName and set the property for each of them. Alternately to avoid a loop and the reliance of timing logic, you could have a single class name (e.g. not-ios) added to an element high up in the document (html or body for example) and make your css selector .not-ios .wrap {...}, then remove this one class when the browser is detected as an iOS device.
|

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.