document.getElementsByTagName('div')
returns an array of nodes (a HTMLCollection
) whose HTML element is <div>
. <!DOCTYPE html> <html> <head> <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> <title>getElementsByTagName</title> <script type="text/javascript"> function main() { try { var node_list = document.getElementsByTagName('body'); node_list[0].innerHTML = "This is the body element"; } catch(e) {alert (e);} } </script> </head> <body onload='main();'> </body> </html>
document.getElementById('…')
returns the (single) element, if it exists, whose attribute id
has a given value.