Learn to Code With JavaScript by Darren Jones
Author:Darren Jones [Nilson Jacques]
Language: eng
Format: epub
Publisher: SitePoint
Published: 2017-08-08T23:00:00+00:00
Updating the HTML
The easiest way to update the HTML on a page is to use the innerHTML property. This will return all the HTML thatâs enclosed inside that elementâs tags as a string. We can see this by entering the following code into the console:
hello.innerHTML; << "Hello, <em>World!</em>"
The great thing about the innerHTML property is that itâs also writable, so it gives us a convenient way to insert a chunk of HTML inside an element.
To demonstrate this, letâs add some JavaScript code into the JS section on CodePen to give a more personalized greeting. First of all, letâs make sure we have a reference to the heading:
const hello = document.getElementById('greeting');
Next, weâll use a prompt box to ask the user for their name and store it in the variable name:
const name = prompt('What is your name?');
Last of all, weâll replace the innerHTML property with our own personalized greeting, which uses the name variable that weâve just collected from the user:
hello.innerHTML = `Hello, <em>${name}!</em>`;
Notice that weâve used a template literal to produce the HTML. These are incredibly useful when creating chunks of HTML to dynamically insert into a web page, as they allow variables to be inserted directly into them.
If everything went to plan, you should see something like this (but hopefully with your name!):
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9794)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8292)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7775)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6782)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6508)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6376)
Kotlin in Action by Dmitry Jemerov(5061)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4315)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3975)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3761)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3702)
The Ultimate iOS Interview Playbook by Avi Tsadok(3679)
