I want to set attributes to tag without jQuery.
I have to set this dynamically.
I understand in jQuery you just do $('html') but without jQuery, I tried Document.getElementById('html') but does not work.
How can I do this?
I want to set attributes to tag without jQuery.
I have to set this dynamically.
I understand in jQuery you just do $('html') but without jQuery, I tried Document.getElementById('html') but does not work.
How can I do this?
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";
document.documentElement is equivalent, document.getElementsByTagName('html')[0] would be closer to jQuery's version I think (but I haven't trawled through its selector engine to find out).