HTML DOM getAttribute() Method Last Updated : 26 Sep, 2024 Comments Improve Suggest changes Like Article Like Report The HTML DOM getAttribute() method is used to retrieve the value of a specified attribute from an HTML element. It returns the attribute's value as a string or null if the attribute doesn't exist.Note: It will return a null or an empty string if the specified attribute doesn't exist.SyntaxObject.getAttribute(attributename)ParameterParameterDescriptionnameIt is a required parameter with a string type that specifies the name of the attribute that needs to retrieve the value.Example 1: This example illustrates the DOM getAttribute() method that specifies the value of the attribute for the specified name, of an element. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>HTML DOM getAttribute() Method</title> </head> <body> <h1 style="color:green;width:50%;"> GeeksforGeeks </h1> <h2>DOM getAttribute() Method</h2> <br> <button id="button" onclick="geeks()">Submit</button> <p id="gfg"></p> <script> function geeks() { let rk = document.getElementById("button").getAttribute("onClick"); document.getElementById("gfg").innerHTML = rk; } </script> </body> </html> Output:DOM getAttribute() MethodExample 2: This example illustrates the DOM getAttribute() method to retrieve the href attribute value of an element. HTML <!DOCTYPE html> <html> <body> <h1 style="color:green;width:50%;"> GeeksforGeeks </h1> <h2>DOM getAttribute() Method</h2> <a id="gfg" href="www.geeksforgeeks.com"> GeeksforGeeks </a><br><br> <button id="button" onclick="geeks()">Submit</button> <br> <p id="rk"></p> <script> function geeks() { var rk = document.getElementById("gfg").getAttribute("href"); document.getElementById("rk").innerHTML = rk; } </script> </body> </html> Output:DOM getAttribute() MethodWe have a complete list of HTML DOM methods, to check those please go through this HTML DOM Object Complete reference article.Supported Browsers: Google Chrome 1.0Microsoft Edge 12.0Firefox 1.0Opera 8.0Safari 1.0We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript. Comment More infoAdvertise with us B bestharadhakrishna Follow Improve Article Tags : Technical Scripter Web Technologies HTML Technical Scripter 2018 HTML-DOM HTML-Methods +2 More Similar Reads HTML DOM scrollWidth Property The DOM scrollWidth property is used to return the width of an element. This property includes padding and also content that is not visible on the screen due to overflow but does not include a border, scrollbar, or margin. It is a read-only property. Syntax: element.scrollWidth Return Value: It retu 1 min read HTML DOM cloneNode() Method The HTML DOM cloneNode() Method is used to copy or clone a node on which the cloneNode() method is called. For example, a list item can be copied from one list to another using this method.Syntax: yourNode.cloneNode([deep]) Parameters: The [deep] is an optional argument. We can set its value to "tru 3 min read HTML onunload Event Attribute The onunload event attribute works when the document is being unloaded i.e. it occurs when the browser has been closed. It is mostly used when the user opens a link and submits the form and closes the browser window. Basically, it is: Fired when a user navigates away from a page or closes the browse 1 min read HTML DOM exitFullscreen() Method The exitFullscreen() method requests the element which is currently in full-screen mode to be taken out of full-screen mode. If the element is not in full-screen mode, then nothing gets changed. The reverse of this method is requestFullscreen(). Syntax: HTMLElementObject.exitFullscreen() Parameters: 1 min read HTML DOM children Property The DOM children property is used to return an HTML collection of all the child elements of the specified element. The elements in the collection can be accessed by index numbers. It differs from childNodes as childNodes contain all nodes (i.e. it includes text and comment nodes as well) but on the 2 min read HTML DOM button Object The button object in HTML is used to represent a <button> element. The getElementById() method is used to get the button object. Property Values:Property ValuesDescriptionautofocusSets or returns whether a button should automatically get focused on page load.defaultValueSets or returns the def 3 min read HTML DOM scrollLeft Property HTML DOM scrollLeft property is used to return or set the number of pixels an element i.e. scrolled horizontally. If the content of the element doesn't generate a scroll bar, then its scrollLeft value is 0;Â Syntax: It returns the scrollLeft property.element.scrollLeftIt is used to set the scrollLef 3 min read HTML DOM scrollHeight Property The DOM scrollHeight property is used to return the height of an element. This property includes padding and also content that is not visible on the screen due to overflow but does not include a border, scrollbar, or margin. It is a read-only property. Syntax: element.scrollHeight Return Value: It r 1 min read HTML DOM lastElementChild Property The DOM lastElementChild property is used to return the last child element of the specified element or null if there is no last element. It is similar to the lastChild property but the difference is that the lastChild property returns all last-child nodes i.e. it includes text and comment nodes as w 1 min read HTML DOM hasAttributes() Method The HTML DOM hasAttribute() method is a boolean function that returns either true or false but not both. This method returns a true value if the element contains an attribute otherwise, it returns false. It is very useful to know whether the element in the document has attributes or not. Syntax: nod 2 min read Like