Why this code return me undefined in the DOM when i don't put the [0]
var d = document.getElementsByName('msg')[0]
document.write(`<br> ${d.innerText}`)
<div id="msg" name="msg">click here</div>
just trying to understand JS in depth
var d = document.getElementsByName('msg')
document.write(`<br> ${d.innerText}`)
<div id="msg" name="msg">click here</div>
CodePudding user response:
Because get elements will return a collection of elements not a single element try get element by id
CodePudding user response:
You're trying with getElementsByName which is plural. You need to use getElementByName so you'll get only one element as an output.