How to write css code in javascript? (Uncaught TypeError: Cannot set property “height” of undefined)


Tags: javascript,css

Problem :

How to write css code in javascript? (Uncaught TypeError: Cannot set property "height" of undefined)

javascript

document.getElementById("slideshow").getElementsByClassName("arrow").style.height = "86px";

css

#slideshow .arrow{
    height:86px;
    width:60px;
    position:absolute;
    background:url('arrows.png') no-repeat;
    top:50%;
    margin-top: -43px;
    cursor: pointer;
    z-index: 5000;
}


Solution :

The key here is the pluralisation of getElementsByClassName - elements. This method returns an array-like object of elements, not just one element.

To apply the style to each, you need to loop through this array-like object and add the styles to each individual element returned:

var elems = document.getElementById("slideshow").getElementsByClassName("arrow");

for (var i = 0; i < elems.length; i++)
    elems[i].style.height = "86px";

    CSS Howto..

    How to expand vertically web's body when open menu

    How to make Flexbox span multiple lines?

    How to align horizontally list if there is a sublist css

    How to set numerous links in a single line with CSS?

    how to show JS alert on open of each tab of the slider?

    AngularJS ng-show animation cross-fade inside ng-repeat

    css: How to float div according to 2nd div in hierarchy

    How to make an text float next to an image in HTML/CSS?

    How can I divide a div element's content with CSS selector?

    how do i get css source file path from firebug?

    How to “zoom”/“scale” in div's content with css or jquery?

    How to get jQuery UI tabs element panels and set visibility on some of them?

    How to make rotating shape look thick?

    How to make bootstrap row “col-md-4” fullscreen size

    How to write css for that?

    How to add Bootstrap to Fullpage.js

    How to make css two borders?

    How to pin sidebar to the top of page but within its parent element using HTML/CSS only

    How to match a comment node with the Jsoup selector api?

    How to override a filter:none in CSS

    how to do a fade in/out while moving down css animation

    How to convert a specific part of materialize.scss to css

    How to reduce the number of sprites when using css sprite technique

    How to prevent transition to run on page load after height is set with javascript in Safari?

    CSS: How to set up border radius cross browser (only IE8 and IE9 missing ?)

    how to move 2 divs on hover with css

    How do I increase the width and font-size of my twitter feed widget?

    How does free hosting website work? [closed]

    How to format a title attribute with css (setting more width)?

    How to fill a transparent image with a progress bar ? #CSS #HTML #Jquery?