Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 718463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:31:46+00:00 2026-05-14T05:31:46+00:00

why when i write document.getElementByClass(‘home1’).setAttribute(‘style’, ‘background-image:url(img/red_menu.PNG);’); it doesn’t work? i have the element with

  • 0

why when i write

document.getElementByClass('home1').setAttribute('style', 'background-image:url(img/red_menu.PNG);');

it doesn’t work?
i have the element with class="home1"

with document.getElementById('home1')...
works fine
thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:31:47+00:00Added an answer on May 14, 2026 at 5:31 am

    It’s getElementsByClassName, not getElementByClass; details here. Note that IE does not support this function (yet).

    getElementsByClassName returns a NodeList of matching elements (rather than a single element), so:

    var list, index;
    list = document.getElementsByClassName("home1");
    for (index = 0; index < list.length; ++index) {
        list[index].setAttribute(/* ... */);
    }
    

    For this sort of thing, you may want to use a library like jQuery, Prototype, Google Closure, etc., to pave over the various browser differences for you. They can save you a lot of time and trouble compared with dealing with those differences yourself.

    For instance, in jQuery:

    $(".home1").attr(/* ... */);
    

    …applies that attribute (via jQuery#attr) to every element with the class “home1”. Although in your particular case, you’d probably want jQuery#css instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit to answer: I have tried the following $(document).ajaxSuccess (function(ev, xhr, s) { document.write('url:
<document.write(<SCR+IPT TYPE='text/javascript' SRC='+http+(window.location.protocol.indexOf('https:')==0?'s':'')+://+gDomain+/+gDcsId+/wtid.js+'><\/SCR+IPT>); I need to escape the string above in order to add
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),
How many people actually write an SDD document before writing a single line of
Say that I write an article or document about a certain topic, but the
I'd like to write some (java) code that takes a PDF document, and creates
I'm trying to write a query for an advanced search page on my document
Are there any Code Generators that would take a well-formed XML document, and write
I want to read, write and create Spreadsheets in the Open Document Format with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.