senthil
senthil

Reputation:

How to change the "html" element's CSS

html {
    width:100%;
}

How to change the CSS of the html tag dynamically on clicking the button using JavaScript? I mean I want to make the above HTML CSS as written below. But I have to change this dynamically using script. Can we do this?

html {
    width:100%;
    overflow-y: hidden;
}

Upvotes: 3

Views: 7106

Answers (3)

sixthgear
sixthgear

Reputation: 6460

First of all, I would not recommend setting styles on the html element directly. The body tag is meant to be the top node of the DOM display list, and Firefox will interpret styles applied to html as styles applied to body anyway. Other browsers may behave differently.

As beggs mentioned, I would recommend learning one of the popular javascript frameworks. They make things like this (HTML traversing and manipulation) a little easier. As it stands, you can write the following code using standard DOM methods. This requires an element with an id of "button" placed somehwhere in your markup.

<a id="button" href="#">Action!</a>

Add the following to a script tag in <head>, or in an external script (recommended).

window.onload = function(e) {

    var button = document.getElementById("button");

    button.onclick = function(e) {
        document.body.style.overflowY = "hidden";
        return false;
    }
}

Alternatively, if you want to use jQuery:

$(document).ready(function() {
    $("#button").click(function(){ 
        $(document.body).css({ overflowY: "hidden" );
    });
});

Upvotes: 1

beggs
beggs

Reputation: 4195

Seems like you would want to learn how to use a javascript framework/toolkit:

Upvotes: 2

Samuel
Samuel

Reputation: 17171

There's probably a simpler way, but

htmlTags = document.getElementsByTagName("html")
for(var i=0; i < htmlTags.length; i++) {
    htmlTags[i].style.overflowY = "hidden";
}

Hope I remembered everything right.

Upvotes: 5

Related Questions