Working with javascript, I've troubles with a behaviour which is new for me: sometimes, an element may be accessed using its id property as a variable. Looks at the following code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>test</title>
<script>
function main() {
let bd=document.getElementById('body');
let myEl=document.createElement('p');
bd.appendChild(myEl);
myEl.setAttribute('id','pluto');
bd.appendChild(myEl);
myEl=document.createElement('p');
myEl.setAttribute('id','minni');
//minni.textContent='minni'; //!Uncaught ReferenceError: minni is not defined
bd.appendChild(myEl);
minni.textContent='minni';
subroutine();
}
function subroutine() {
console.log(body);
console.log(pippo);
console.log(pluto);
//minni.textContent=minni.id; //!Uncaught ReferenceError: Cannot access 'minni' before initialization
pippo.textContent='pippo';
let minni = document.getElementById('pluto');
minni.textContent=minni.id;
console.log(minni);
}
</script>
</head>
<body id="body" onload=main()>
<p id='pippo'>
</body>
</html>
Where is referenced this behaviour?
I think this behaviour is not documented anywhere.
May you give me some valid reference?