Solution 1 :

var whatIWantedToSelect = document.html;

The HTML element is the document.documentElement.

var whatIWantedToSelect = document.getElementsByName(‘html’)[0];

getElementsByName matches elements by their name attribute. You are looking for getElementsByTagName.

Solution 2 :

You can use getElementsByTagName function as follows:

let html = document.getElementsByTagName('html')[0];

Solution 3 :

The root <html> element is available as document.documentElement. So there is no need to select it by tag name (which would have worked too, but you had getElementsByName instead of getElementsByTagName).


var whatIWantedToSelect = document.documentElement

Problem :

I want to get the element using javascript without using its ID( html id=”somethin” ) or a class.

Something like…..

var whatIWantedToSelect = document.html;

Something like…..

var whatIWantedToSelect = document.getElementsByTagName('html')[0];

Please, see the below picture to see the exact DOM element that I want to access via javascript.

The DOM element that I want to select


Comment posted by Lakshitha Kanchana

but my event listener is not working when document.getElementsByTagName(‘html’)[0].click(); called. But if I clicked somewhere b mouse it works, Why?