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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:12:48+00:00 2026-06-02T10:12:48+00:00

document.getElementById(‘myId’).style; is one way of accessing the style attribute.. Also we can do the

  • 0
document.getElementById('myId').style;

is one way of accessing the style attribute..
Also we can do the same using
document.getElementById('myId').getAttribute('style');

What is the difference between these two ways of getting attribute values..And which one is preferable?

  • 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-06-02T10:12:51+00:00Added an answer on June 2, 2026 at 10:12 am

    In the first example you’re not accessing to the style attribute, but to the style property. The property’s value can be anything, in case of the style property is an object. In the second example you’re accessing to the style attribute of the tag. The attribute’s value can be only string.

    In case of some attributes there is a mapping between them. So if you set an attribute style on a HTML node, your style property is updated and your style is applied. However, this is not always true: a well known bug in some versions of IE (at least till IE7) is that sort of mapping is broken, so set an attribute is not reflected to the property.

    So, if you want set an attribute on a HTML node, you have to use the second one. But if you want to access to the property of your object that represent a HTML node, you have to use the first one.

    In case of the style, the first one is strongly recommended.

    To make it clear with an example (in modern browsers):

    document.body.style.border = "1px solid red";
    console.log(document.body.style); // [object CSSStyleDeclaration]
    console.log(document.body.getAttribute("style")); // "border: 1px solid red;"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

and in JavaScript document.getElementById('myId'); This is not working in JSF application. However, the same
document.getElementById(...).setAttribute('style',... is not working in Internet Explorer 7.0. How can I make this work
Using document.GetElementById(thingieID) doesn't seem to work. Can anyone help this DOM newbie?
var cookieValue = document.getElementById(demo); var value = cookieValue .getAttribute('value'); if(typeof(Storage)!==undefined) { alert(value); localStorage.setItem =
document.getElementById('logbox').scrollTop = document.getElementById('logbox').scrollHeight; I tried $('#logbox').scrollTop but there is not this attribute. How to
document.getElementById(Iframe_id).contentWindow.addEventListener(blur, blurtest, true); By using this line I have given the blur event to
var tbl = document.getElementById(TABLE_NAME); var nextRow = tbl.tBodies[0].rows.length; row.setAttribute('style', cursor: pointer;); This will add
var fu1 = document.getElementById(FileUpload1); How can I get the filename of the fileupload control
iFrame.document.getElementById('Formname').disabled = true; This is not working with Netscape browsers(FF,Chrome and Safari). Can some
I am using document.getElementById(password).value to recieve the password type input field. please help out!

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.