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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:58:07+00:00 2026-06-12T12:58:07+00:00

We have a setAttribute method, for DOM elements. https://developer.mozilla.org/en-US/docs/DOM/element.setAttribute How is it different from

  • 0

We have a setAttribute method, for DOM elements.

https://developer.mozilla.org/en-US/docs/DOM/element.setAttribute

How is it different from using the below?

 domElement.propName = value

Is there any benefit to either approaches ?

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-06-12T12:58:08+00:00Added an answer on June 12, 2026 at 12:58 pm

    domElement.setAttribute('propName', obj) is setting an XML attribute, it will be converted into a string and added to the DOM tag.

    domElement.propName is setting an expando property, it can be of any type. It’s setting it on the JS object that wraps the DOM object implementation.

    They do not have the same effect, unless you are dealing with an attribute that is recognized by the parser like src,id,value. Those properties get copied to the expando property but there are many rabbit holes and cases where it doesn’t work reliably (usually when the expando doesn’t take a string, like onclick, checked)

    This example shows that they are different.

    domElement.setAttribute('someProp', 5);
    console.log(domElement.someProp); // undefined
    domElement.someProp = 10; 
    console.log(domElement.someProp); // 10
    console.log(domElement.getAttribute('someProp')); // "5" -> it's a string 
    

    Always using DOM expando properties is less likely to cause trouble. The only case where you want to use setAttribute is when you need to serialize the node (using outerHTML) and you want that attribute to be reflected in the serialization

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

Sidebar

Related Questions

I have a method that uses setAttribute on the Request object. I need to
Basically, I have my DOM objects set up and am calling the .getAttribute method
i have one question i'm using @RequestMapping(value = /user, method = RequestMethod.GET) public String
Why does GWT have two element types? you have com.google.gwt.dom.client.Element & com.google.gwt.user.client.Element - when
I have a Javascript plugin that searches the DOM for any elements starting with
See subject title. I have a method in Java that draws a TextBlock element:
I have a web application that maintains a version flag as a ServletContext.setAttribute('flag', flagValue)
i have a QMainWindow. It has this parameters: this->setWindowFlags(Qt::Tool); this->setFocusPolicy(Qt::StrongFocus); this->setAttribute(Qt::WA_QuitOnClose,true); After showEvent calles
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
I have been experimenting with using javascript to dynamically write stylesheets. In Firefox, one

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.