0

I was learning JS From W3 schools and encounter this program which checks the attribute length. I later modified it to display name of attribute that an element has. Here is the code

function myFunction() {
  for (var counter = 0; counter < 2; counter++)
    var a = document.getElementById("myBtn").attributes[counter].name;
  document.getElementById("output").innerHTML = a;
}
<html>

<body>
  <p>Click the button to see how many attributes the button element have.</p>
  <p id="output">

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

</html>

I expected O/P to be as :

id onclick

But for some reason it only display the last attribute that an element holds in this case its onclick if it were class or something else it would have displayed that.

Whats the reason for that ?

1
  • 1
    you are runnig loop its getting overwrite Commented Jan 17, 2017 at 13:08

4 Answers 4

2

You have a very simple mistake in your code. Your code will first set the innerHTML of the <p> element to "id". Then, it will overwrite it with the next attribute, i.e. "onclick". To fix this, change

document.getElementById('output').innerHTML = a

to

document.getElementById('output').innerHTML += a + "<br>"

The <br> is just to seperate the attributes from each other. Here is the whole snippet:

function myFunction() {
  for (var counter = 0; counter < 2; counter++) {
    var a = document.getElementById("myBtn").attributes[counter].name;
    document.getElementById("output").innerHTML += a + "<br>";
  }
}
<html>

<body>
  <p>Click the button to see how many attributes the button element have.</p>
  <p id="output">

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

</html>

Sign up to request clarification or add additional context in comments.

Comments

1

Try Below instead

function myFunction() {
var a="";
  for (var counter = 0; counter < 2; counter++){
     a+= document.getElementById("myBtn").attributes[counter].name;
}
  document.getElementById("output").innerHTML = a;
}

Your code displays the last value set to a. And you do not append / add the names of the attributes to a, instead you overwrite a with

var a = ....

Comments

1

This happens because you change the innerHTML of the element, which means you change the whole HTML of that element.

To do what you want, you could concatenate the results on the current innerHTML or create elements and add the values inside of them, then append them to the document.

For example:

var output = document.getElementById('output');
output.innerHTML = output.innerHTML + ' - ' + a;

Comments

1

At the moment, you are re-assigning a each time the loop runs (var a = ...):

function myFunction() {
  for (var counter = 0; counter < 2; counter++) {
    var a = document.getElementById("myBtn").attributes[counter].name;
  }
  document.getElementById("output").innerHTML = a;
}

This means that a ends up being the last item.

Perhaps instead, add each attribute to an array:

function myFunction() {
  var arr = [];
  for (var counter = 0; counter < 2; counter++) {
    arr.push(document.getElementById("myBtn").attributes[counter].name);
  }
  document.getElementById("output").innerHTML = arr.join(' ');
}

Also, use curly braces for where your scopes are. You want that output to be after the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.