As mentioned by Quynh Nguyen, you don’t need the ‘.’ in the className. However – document.getElementsByClassName(‘col1’) will return an array of objects.
This will return an “undefined” value because an array doesn’t have a class. You’ll still need to loop through the array elements…
function changeBGColor() { var cols = document.getElementsByClassName('col1'); for(i = 0; i < cols.length; i++) { cols[i].style.backgroundColor = 'blue'; } }
Related Posts:
- How can I scroll to an element using jQuery?
- Uncaught TypeError: Cannot read property ‘top’ of undefined
- Retrieve the position (X,Y) of an HTML element
- Cannot read property ‘style’ of undefined — Uncaught Type Error
- Failed to load resource: the server responded with a status of 404 (Not Found)
- Failed to load resource: the server responded with a status of 404 (Not Found)
- How to change CSS using jQuery?
- Angular: conditional class with *ngClass
- Remove class using jQuery
- jQuery document.createElement equivalent?
- Include another HTML file in a HTML file
- Remove element by id
- “Uncaught TypeError: Cannot read property ‘style’ of null at.. “
- How to resolve the C:\fakepath?
- What is the meaning of == $0 that is shown in inspect element of google chrome for the selected element [duplicate]
- Bootstrap Dropdown menu is not working
- Change Background color (css property) using Jquery
- Remove all child elements of a DOM node in JavaScript
- Using Javascript in CSS
- Set timeout for ajax (jQuery)
- querySelector vs. getElementById
- How to generate a simple popup using jQuery
- How does the data-toggle attribute work? (What’s its API?)
- CSS height 100% percent not working
- JavaScript get element by name
- Use images like checkboxes
- Adding images to a Bootstrap Carousel with AJAX
- Completely disable scrolling of webpage
- Angular: conditional class with *ngClass
- Find an element in DOM based on an attribute value
- React Error: Target Container is not a DOM Element
- Setting background-image using jQuery CSS property
- scrollIntoView Scrolls just too far
- Difference between innerText, innerHTML and value?
- How do I set/unset a cookie with jQuery?
- Angular: conditional class with *ngClass
- How do you Hover in ReactJS? – onMouseLeave not registered during fast hover over
- How can I check if an element exists in the visible DOM?
- How can I select an element by name with jQuery?
- Full-screen iframe with a height of 100%
- How to add multiple classes to a ReactJS Component?
- Change CSS of class in Javascript?
- How to print HTML content on click of a button, but not the page?
- How can I add a class to a DOM element in JavaScript?
- Check if an element contains a class in JavaScript?
- Equivalent of jQuery .hide() to set visibility: hidden
- Bootstrap 4 Dropdown Menu not working?
- Parse an HTML string with JS
- TypeError: $(…).modal is not a function with bootstrap Modal
- Why is the jquery script not working?
- How to do fade-in and fade-out with JavaScript and CSS
- TypeError: $(…).DataTable is not a function
- Change text on hover, then return to the previous text
- CSS Animation onClick
- Textarea Auto height
- Mixed Content Page: requested an insecure stylesheet error
- Why is marquee deprecated and what is the best alternative?
- For loop for HTMLCollection elements
- Refresh/reload the content in Div using jquery/ajax
- How to write inside a DIV box with javascript
- jQuery count child elements
- How to get height of div in px dimension
- How do I check if an element is hidden in jQuery?
- Crop the image using JavaScript
- document.getElementbyId() returning null
- Force div to be topmost element of any and all webpages
- How to display raw JSON data on a HTML page
- Javascript: ‘window’ is not defined
- “Uncaught TypeError: a.indexOf is not a function” error when opening new foundation project
- how to display a div triggered by onclick event
- Why is jquery show() not working in example
- Invariant Violation: _registerComponent(…): Target container is not a DOM element
- jQuery scrollTop() method not working
- Set CSS property in JavaScript?
- Change an image with onclick()
- JavaScript – Count Number of Visitor for Website
- Creating Accordion Table with Bootstrap
- Fully responsive HTML5 video
- How to create checkbox inside dropdown?
- How to change cursor from pointer to finger using jQuery?
- Positioning element at center of screen
- Adding Google Translate to a web site
- Bootstrap Collapse not Collapsing
- How can I remove a style added with .css() function?
- How to make HTML element resizable using pure Javascript?
- How to use aria-expanded=”true” to change a css property
- How to set “style=display:none;” using jQuery’s attr method?
- WordPress Bootstrap Handburger Menu Wont Open
- Correct way to include extra styles & script files?
- WP adds long version query strings to CSS and JS files
- WordPress getting css and script files from client machine rather than the server machine
- Hide CSS class only in single post pages
- How to keep close a sub-menu under homepage? [closed]
- What is the difference between call and apply?
- How would I call a javascript function in html?
- How do you serve a file for download with AngularJS or Javascript?
- How can I cache image files locally with PhoneGap / Cordova?
- get_header() in backend – but keep JavaScript and CSS files
- How can I put this JavaScript into WordPress? [closed]
- How to show cities according to the state in Form using Javascript? [closed]