And if you want to modify the innerHTML property of the element, it can be achieved in the following way: This will give us 'Hello World ' as a string. To access the innerHTML you’ll first need to access the element itself with getElementById():Ĭonst someDivElement = document.getElementById(“someDivElement”) Take the following HTML example: Hello World Code language: HTML, XML ( xml )
InnerHTML is an HTML element property that has two uses for web developers:ġ) You can use it to get the internal HTML content of any HTML element as an HTML string.Ģ) You can also use it to set or change elements’ innerHTML content. This blog post will walk you through the pros and cons of using the innerHTML property and how you can use it safely without opening the doors to potential Cross-Site scripting (XSS) attacks.
Luckily for you, we can associate these dynamic pieces of information with HTML elements and easily display their current state using the innerHTML property. Whether it’s showing stock quotes, the current time, the user’s name, or any other piece of info that changes with time or user, you’ll want to make sure you’re regularly updating this dynamic content to produce the best experience for your users.
If you’re creating any dynamic web page these days, making sure you have relevant and up-to-date information is vital.