How to get attribute name as nodeName return?

Hello!
I am new here and I try to get a name of an attribute as a return value for nodeName.

Here is the code I tried, I only get element node names and #text, but attribute name never:

<!DOCTYPE html>
<html>
<body>

<a href="https://www.w3schools.com">Visit W3Schools</a>

<h1 id="id01">My First Page</h1>
<p id="id02"></p>

<script>
document.getElementById("id02").innerHTML = document.body.childNodes[].nodeName;
</script>

</body>
</html>

Many thanks!!!

Can you explain a bit more what you’re trying to do here, and what you think the line of code should be doing? A node name is a string. And it’s just a [read-only] property, it can’t return anything else. So just need a bit more clarity re your intent - what attribute, for example?

At the minute you are selecting the element with ID id02 and as far as I can see, attempting to set the HTML inside it to the node name of one (?) of the nodes in the document - there’s an issue with your syntax there but I don’t want to offer any advice before I’m sure I understand what you’re trying to do.

I am trying to get the word “href” as an answer (as the name of the attribute node href.)

I put into the p element id id02 the name of the nods I go through (changing numbers in [] , that is why i left it empty

<!DOCTYPE html>
<html>
<body>

<a href="https://www.w3schools.com">Visit W3Schools</a>

<h1 id="id01">My First Page</h1>
<p id="id02"></p>

<script>
document.getElementById("id02").innerHTML = document.body.childNodes[0].nodeName;
</script>

</body>
</html>

Here I get #text, while this is inside element, then I want to proceed to other children and I only get text and element nodes, but never attributes

Right, so node name, all you can get is the name. It’s like an array, and asking for array.length - all you can get is the length, it doesn’t lead to anything else.

What is the.purpose of getting the name of the attribute (note this has nothing to do with node names), and do you you want the attributes for the element with that ID? If you want all the attributes of an element, then

myElement.attributes

Will give you a nodelist of all of them

for (let [key, val] of myElement.attributes) {
  console.log(key)
}

Or do you want to check if an element has an href and print href if so?

if (myElement.href)
  console.log(myElement.href)
else
  console.log('No href!')

Hello, thank you very much! But it seems a little complicated for the beginner, doesnt it? Is it not possible to get it just using nodeName?

I’m not sure how to explain this more clearly. nodeName is a read-only property of DOM nodes. It’s just a capitalised string that is a code that represents the element type, so for a <p> it is P, for a <div> it is DIV and so on. It gives you absolutely no other information, you can’t find out any attributes or whatever from it. Unless I’m missing something here, the node name has zero use to you.

If you have an element selected, you can get its attributes via theElementISelected.attributes. With an href, you can get the value of the href property using theElementISelected.href

Again though, why are you trying to get the name of the property href?