I have an element:
JavaScript
x
2
1
<div id="Form" title="Disability Form</div>
2
I am trying to get both the id and the title, formatted as such:
JavaScript
1
3
1
id: Form
2
title: Disability Form
3
I’m also trying to use pure Javascript. This is what I have been playing with so far:
JavaScript
1
7
1
var x = document.getElementsByTagName("div");
2
3
for(i=0; i<= x.length; i++)
4
{
5
console.log(x[i].innerText);
6
}
7
Does anyone know how to proceed?
Advertisement
Answer
Mistakes that you have made
<=
should be changed to<
- Didn’t close the tag properly
To get the id
you can use element.id
To get the title
you can use element.getAttribute('title')
JavaScript
1
8
1
var x = document.getElementsByTagName("div");
2
3
for(i=0; i< x.length; i++)
4
{
5
console.log(x[i].innerText);
6
console.log('id : ' + x[i].id);
7
console.log('title : ' + x[i].getAttribute('title'));
8
}
JavaScript
1
1
1
<div id="Form" title="Disability Form"></div>