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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:33:25+00:00 2026-06-02T08:33:25+00:00

Expressions like Element.getAttribute(id) and Element.id return the same thing. Which one should be used

  • 0

Expressions like Element.getAttribute("id") and Element.id return the same thing.

Which one should be used when we need attributes of an HTMLElement object?

Is there any cross browser issue with these methods like getAttribute() and setAttribute()?

Or any impact on performance between directly accessing object properties vs using these attribute methods?

  • 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-02T08:33:27+00:00Added an answer on June 2, 2026 at 8:33 am

    getAttribute retrieves the attribute of a DOM element, while el.id retrieves the property of this DOM element. They are not the same.

    Most of the time, DOM properties are synchronized with attributes.

    However, the synchronization does not guarantee the same value. A classic example is between el.href and el.getAttribute('href') for an anchor element.

    For example:

    <a href="/" id="hey"></a>
    <script>
    var a = document.getElementById('hey')
    a.getAttribute('href') // "/"
    a.href // Full URL except for IE that keeps '/'
    </script>
    

    This behavior happens because according to the W3C, the href property must be a well-formed link. Most browsers respect this standard (guess who doesn’t?).

    There is another case for the input‘s checked property. The DOM property returns true or false while the attribute returns the string "checked" or an empty string.

    And then, there are some properties that are synchronized one-way only. The best example is the value property of an input element. Changing its value through the DOM property will not change the attribute (edit: check the first comment for more precision).

    Because of these reasons, I’d suggest you keep using the DOM properties, and not the attributes, as their behavior differs between the browsers.

    In reality, there are only two cases where you need to use the attributes:

    1. A custom HTML attribute, because it is not synced to a DOM property.
    2. To access a built-in HTML attribute, which is not synced from the property, and you are sure you need the attribute (for example, the original value of an input element).

    If you want a more detailed explaination, I strongly suggest you read this page. It will take you a few minutes only, but you will be delighted by the information (which I summed up here).

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

Sidebar

Related Questions

I really need some help in Regular Expressions, i'm working on a function like
I have always written my boolean expressions like this: if (!isValid) { // code
How would I go about joining two lambda expressions like theese: Expression<Func<string, bool>> expr1
I would like to be able to store a list of expressions to execute
I'm exploring the power of regular expressions, so I'm just wondering if something like
I have a set of expressions representing some formula with some parameters inside. Like:
I'm new to regular expressions, and have no clue where to start, it's like
I've got some dynamically-generated boolean logic expressions, like: (A or B) and (C or
I'm creating a Shell Script, and I have a file like this called expressions.txt
I've created an F# class to represent an array that allocates one element for

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.