How to use DOM Element hasAttribute() method in Javascript?

Find out how to use the JavaScript hasattribute() method to determine if a DOM element has a specific attribute.

This article provides a comprehensive guide that includes clear explanations and code examples so that it is easy for you to use hasattribute in JavaScript.

This tutorial will cover its syntax, parameters, return values, and practical examples to help you understand its usage.

What is hasAttribute() method in JavaScript?

The hasAttribute() is a built-in method in JavaScript that is part of the DOM API. It helps you find out if a specific attribute exists on an HTML element.

This method returns a boolean value when called, true if the attribute exists, and false if it doesn’t.

In a simple understanding, the hasAttribute() method tells you if a specific attribute exists or not. It returns true if the attribute is found, and false if it’s not.

Here’s the syntax for hasAttribute() method:

element.hasAttribute(attributeName);

In this case, the term “element” represents the HTML element you want to check if the attribute exists.

The “attributeName” is a string that specifies the name of the attribute you like to verify.

Note: You can use the setAttribute() method to add a new attribute or change the value of an existing attribute on an element.

Parameters

The hasAttribute() method takes only one parameter:

The attributeName parameter is important and needs to be a string. It should contain the name of the attribute you want to check. Remember, the attribute name is case-sensitive.

Return Value

The hasAttribute() method returns a boolean value:

✅ true

If the attribute you specified exists on the element.

✅ false

If the attribute you specified does not exist on the element.

JavaScript hasAttribute() Example code

Here’s the HTML code:

!DOCTYPE html>
<html>
<body>

<p>Try to click the button to check if it has the "onclick" attribute.</p>

<button id="elem" onclick="change()">Try it</button>

<script>
function change() {
  var a = document.getElementById("elem")
  var b = a.hasAttribute("onclick");
  console.log(b);
}
</script>

</body>
</html>

Here’s the Web View:

javascript hasattribute

When we click the button, the console shows the result as “true.” This means that the button element with the ID “elem” has the attribute we passed as a parameter.

Here’s the console output:

true

On the other hand, you can also use the following HTML sample code:

<!DOCTYPE html>
<html>
<body>

<p>Try to click the button to check if it has the "onclick" attribute.</p>

<button id="myBtn" onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var sample = document.getElementById("myBtn").hasAttribute("onclick");
    document.getElementById("demo").innerHTML = sample;
}
</script>

</body>
</html>

Conclusion

In conclusion, this article explains how to use the hasAttribute() method in JavaScript.

This method helps you find out if a specific attribute exists on an HTML element. When you call the hasAttribute() method, it returns true if the attribute is found and false if it’s not.

The article also provides the syntax for using the method, which involves specifying the HTML element you want to check and the name of the attribute you want to verify.

Moreover, article offers a clear and straightforward explanation of how to determine if a DOM element has a specific attribute using JavaScript.

If you’re confused about whether the DOM Element hasAttribute() method and the hasAttribute method in JavaScript are the same, the answer is yes.

The hasAttribute() method is a method of the DOM Element interface in JavaScript.

When people refer to the hasAttribute method in JavaScript, they are usually referring to the hasAttribute() method of the DOM Element interface.

We are hoping that this article provides you with enough information that helps you understand the JavaScript hasattribute() method.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment