Chain
Chain

Reputation: 627

Setting the overflow property with Javascript

i need this function to enable vertical scrolling but not horizontal.

This doesn't works:

function scrollFunction() {
        // Scroll to top 
    document.getElementsByTagName('body')[0].style.overflow-y='auto';
}

While this does, but it enables both vertical and horizontal scrolling.

function scrollFunction() {
    // Scroll to top
    document.getElementsByTagName('body')[0].style.overflow='auto';
}

How can I specify just the overflow-y properly? Thanks

Upvotes: 4

Views: 12260

Answers (2)

Wryte
Wryte

Reputation: 895

As an alternative to VisioN's answer you can use square brackets and a string to access it:

document.getElementByTagName("body")[0].style['overflow-y'] = "auto";

This is good if you don't know what the camelCase is.

Upvotes: 4

VisioN
VisioN

Reputation: 145388

Compound style properties are written in camelCase notation (the first letter is small):

document.getElementsByTagName("body")[0].style.overflowY = "auto";

Upvotes: 10

Related Questions