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 6131359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:57:53+00:00 2026-05-23T16:57:53+00:00

I have this javascript code: var elt = document.createElement(div); elt.class = class_1; There is

  • 0

I have this javascript code:

var elt = document.createElement("div");
elt.class = "class_1";

There is supposed to be a styling associated with .class_1 in my CSS, but it did not really apply for some reason. After spent a few hours figuring out what went wrong, I tried this alternative:

elt.setAttribute("class", "class_1");

and it worked….

It is weird since on the other part of my code, I used elt.id and it worked just fine. At first I thought it was a cross-browser issue, but it turned out that "elt.class" doesnt work in all browser.

Is this a bug in the native javascript DOM? can somebody explain why this is or if I did anything wrong? Thanks. All inputs/answers would be appreciated.

  • 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-23T16:57:54+00:00Added an answer on May 23, 2026 at 4:57 pm

    Properties and attributes are different things. Attributes are the strings you see in the document code, accessible for any type of document through the DOM Core method getAttribute:

    <a id="foo" href="bar" tabindex="1" onclick="alert()" class="bof" potato="lemon">
    
    a.getAttribute('id')         // string "foo"
    a.getAttribute('href')       // string "bar"
    a.getAttribute('tabindex')   // string "1"
    a.getAttribute('onclick')    // string "alert()"
    a.getAttribute('class')      // string "bof"
    a.getAttribute('potato')     // string "lemon"
    

    whereas the properties, accessible directly on the object, are specialised and document-type-specific (eg defined in DOM HTML or HTML5) and are generally more readable convenience facilities. Although they often reflect the same thing as the attribute, they can differ in terms of name, value, type or purpose.

    a.id                         // string "foo"
    a.href                       // string "http://www.example.com/base/bar"
    a.tabIndex                   // integer 1
    a.onclick                    // function() { alert(); }
    a.className                  // string "bof"
    a.potato                     // undefined
    

    So whilst id results in the same value, href and all other URL properties get absolutised relative to the document; tabIndex and other numeric properties returns a Number instead of literal string; onclick and other event handler properties return a JS Function object, and properties whose name happens to clash with common reserved words like class (not just only JavaScript reserved words, as DOM is a cross-language standard) get renamed (see also: htmlFor), and non-standard attributes aren’t accessible as properties at all. Other gotchas: value/selected/checked on inputs reflect the current contents of a form field; the attributes of the same name are the defaultValue/defaultSelected/defaultChecked initial values.

    [Aside: IE<8 has real trouble telling the difference between attributes and properties, and will return the wrong thing for getAttribute in all cases where they differ. Don’t let this confuse you, and avoid using getAttribute for accessing HTML DOM properties for this reason.]

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

Sidebar

Related Questions

I have this javascript code: var img = document.createElement(img); img.setAttribute('src', 'close.png'); img.setAttribute('alt', 'cancel this
I have the following JavaScript code: var ans_el = document.createElement( 'input' ); ans_el.setAttribute( 'id',
I have this JavaScript code: for (var idx in data) { var row =
I have this piece of code in javascript: var catId = $.getURLParam(category); In my
Okay, I have this code: <script language=javascript type=text/javascript> <!-- function requestPage(page) { var request
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
i have this js code : var str = javascript:__doPostBack('ctl00$M$List$_rli2$ctl06',''); alert (str); var str
i have this code in javascript: var object = { get: function(id){ sel =
I have this JavaScript code: <script type=text/javascript> function timeMsg() { var t=setTimeout(doRedirect(),10000); } function
I have this Javascript problem I cannot solve. My code is: <script type=text/javascript> var

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.