views:

183

answers:

4
html {
    width:100%;
}

How to change the css of above mentioned html 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;
}
+1  A: 

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.

Samuel
ah thanks i dont think i ever set style properties with dashes before
Samuel
This example, if it works (I didn't bother to check, and can't remember offhand), would benefit from simply pulling the first value, as there should (ideally) only ever be one HTML tag.htmlTags = document.getElementsByTagName("html")[0].style.overflowY = "hidden";
Ryan McGrath
+2  A: 

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

beggs
+1. Learning jQuery is seriously time well spent!
Dave Markle
+3  A: 

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" );
    });
});
sixthgear
A: 

Thanx for all..... I used jQuery framework as sixthgear mentioned... one more thing.. position fixed is not working in ie6 browser alone... Do u have any idea to fix tat?

Ask a new question, please
Josh Stodola