Basic JavaScript output and embedded writing tutorials, javascript embedded
JavaScript does not have any printing or output functions.
In HTML, JavaScript is usually used to operate HTML elements.
Operate HTML elements
To access an HTML element from JavaScript, you can use the document. getElementById (id) method.
Use the "id" attribute to identify HTML elements and innerHTML to obtain or insert element content:
Instance
<! DOCTYPE html> <body>
The preceding JavaScript statement (in the <script> tag) can be executed in a web browser:
- Document. getElementById ("demo") is the JavaScript code that uses the id attribute to search for HTML elements.
- InnerHTML = "Paragraph changed." Is the JavaScript code used to modify the HTML content (innerHTML) of an element.
Write to HTML document
For testing purposes, you can write JavaScript directly in HTML documents:
Instance
<! DOCTYPE html> <body>
Use document. write () to output only the written content to the document.
If you execute document. write after the document has been loaded, the entire HTML page will be overwritten.
Instance
<! DOCTYPE html> <body>
Write to console
If your browser supports debugging, you can use console. log () to display JavaScript values in your browser.
Use F12 in the browser to enable the debugging mode. In the debugging window, click the "Console" menu.
Instance
<! DOCTYPE html> <body>