JavaScript Display Possibilities
JavaScript has various methods to present data:
- Inserting content into an HTML element with innerHTML.
- Outputting content directly to the HTML document via document.write().
- Showing content in an alert dialog using window.alert().
- Logging content to the browser console through console.log().
Using innerHTML
JavaScript accesses HTML elements using the document.getElementById(id) method.
The id attribute specifies the HTML element, while the innerHTML property determines its content.
Example
<!DOCTYPE html> <html> <body>
<h1>My First Web Page</h1> <p>My First Paragraph</p>
<p id=”demo”></p>
<script> document.getElementById(“demo”).innerHTML = 5 + 6; </script>
</body> </html>
|
Modifying the innerHTML property of an HTML element is a frequently used method to showcase data within HTML. |