How to convert $() into document.getElement? [duplicate]

Solution 1:

In the general case, the standard DOM equivalent to jQuery('element_name'); is document.getElementsByTagName('element_name');. Note that it returns a NodeList (which is like an array) and not just an HTMLElementNode.

The HTML element, as the root element, can be accessed via document.documentElement.

Setting attribute values can be done with the setAttribute('attribute_name', 'attribute_value'); method on an HTMLElementNode. The method is buggy in older versions of Internet Explorer, so you may wish to use the equivalent DOM property instead.

For example, to replace the value of the class attribute:

document.documentElement.className = "foo bar baz";

Solution 2:

document.getElementsByTagName('html')[0].setAttribute('name','value');