What does ".innerHtml" do? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What does ".innerHtml" do?

When looking at other people's web scripts they have this method or property called ".innerHtml", but I don't understand what that means so can someone explain it to me?

17th Sep 2017, 3:45 PM
Bryan
Bryan - avatar
6 Answers
+ 10
if you had something in a document like: <p id=bread> I don't like bread </p> then in JavaScript... var change = document.getElementById("Bread"); using "change.innerHtml = "<h1> I like bread </h1> will change the paragraph tag to a h1 tag and the text to "I like bread". but for more information on this you can look at it on the JavaScript Sololearn tutorial (specifically in DOM & Events) Edit: No Problem :)
17th Sep 2017, 3:54 PM
LynTon
LynTon - avatar
+ 5
The 'innerHTML' (case is important) property provide read/write access to an element Html source code content... If you have this Html code: <div id="mydiv"> this is some text content, and <code>some other child element</code> </div> ... and you get the reference of <div> element in variable 'mydivref', you could read and/or write the text value of Html source code inner content ("this is some text content, and <code>some other child element</code>" -- ie: including child element tags) and so do dyamical change of your Html page: var mydivref=document.getElementById('mydiv'); alert(mydivref.innerHTML); mydivref.innerHTML="new content including parsed/interpreted <code>html source code</code> as well"; ... will display actual content of div in an alert box, and then replace it with new one assigned, to be displayed as if Html loaded was (is now): <div id="mydiv"> new content including parsed/interpreted <code>html source code</code> as well </div>
17th Sep 2017, 4:02 PM
visph
visph - avatar
+ 4
.innerHtml is used to change inner content of any html tag by JavaScript. https://stackoverflow.com/questions/4879066/what-innerhtml-is-doing-in-javascript
17th Sep 2017, 4:01 PM
Gaman Tyagi
Gaman Tyagi - avatar
+ 3
Thanks
17th Sep 2017, 3:55 PM
Bryan
Bryan - avatar
+ 2
If you want to print that message on the same page where you button is. Then you go for .innerHtml. It helps to print the text on the same page whenever you click on button
17th Sep 2017, 3:58 PM
kanishk goel
kanishk goel - avatar
+ 2
Thanks, all of you, the examples and explanations that you are giving are really helpful. In fact it gives me an idea for a piece of code I will start working on now, to test that out! Thank you.
17th Sep 2017, 4:07 PM
Bryan
Bryan - avatar