I recently came across these two methods for dom elements retrieval in a douglas crockford presentation but couldn't understand the difference between these two.
document.getElementsByTagName()
document.getElementsByName()
can someone please explain it to me.
the link to the video is http://www.youtube.com/watch?v=Fv9qT9joc0M
Suppose you have this HTML :
<input name="test" class="cssclassname">
You'd got it with
document.getElementsByTagName('input')
or
document.getElementsByName('test')
or
document.getElementsByClassName('cssclassname')
Also, you can call getElementsByTagName
on elements other than document. For example the following is allowed,
document.getElementsById('foo').getElementsByTagName('bar')
But getElementsByName
can only be called on document
.
Notes :
document.getElementsByTagName('input')[0]
<div name="alpha"></div>
<div name="beta"></div>
var divs = document.getElementsByTagName("div"); // Selects both divs.
var alpha = document.GetElementsByName("alpha"); // Selects the first div.
var beta = document.GetElementsByName("beta"); // Selects the second div.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With