I have an element:
<div id="Form" title="Disability Form</div>
I am trying to get both the id and the title, formatted as such:
id: Form title: Disability Form
I’m also trying to use pure Javascript. This is what I have been playing with so far:
var x = document.getElementsByTagName("div"); for(i=0; i<= x.length; i++) { console.log(x[i].innerText); }
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')
var x = document.getElementsByTagName("div"); for(i=0; i< x.length; i++) { console.log(x[i].innerText); console.log('id : ' + x[i].id); console.log('title : ' + x[i].getAttribute('title')); }
<div id="Form" title="Disability Form"></div>