Output – JavaScript html basics

Output – JavaScript basics

You can use JS to display the output in different ways. Use window.alert() This function is used to display message alerts.
<!DOCTYPE html>
<html>
<body>
<h1>Code Projects</h1>
<p>Projects, Tutorials and More </p>
<script>
window.alert("ALERT");
</script>

</body>
</html>
Output:

Insert image description here

Use innerHTML
The document.getElementById(id) method can be used in JS to access HTML elements. The Html element is defined by the id attribute, and the HTML content is defined by innerHTML.
<!DOCTYPE html>
<html>
<body>
<h1>Code Projects</h1>
<p>Projects, Tutorials and More </p>
<p id="ex"></p>

<script>
document.getElementById("ex").innerHTML = 50 + 50;
</script>

</body>
</html>
Output:Insert image description here

Use document.write( )

<!DOCTYPE html>
<html>
<body>
<h1>Code Projects</h1>
<p>Projects, Tutorials and More </p>
<script>
document.write(50 + 50);
</script>

</body>
</html>

Insert image description here

document.write() after an HTML document will delete any existing HTML content:
<!DOCTYPE html>
<html>
<body>
<h1>Code Projects</h1>
<p>Projects, Tutorials and More </p>
<button type="button" onclick="document.write(50 + 50)">Click</button>

</body>
</html>
Output:Insert image description here

Insert image description here

Existing html content will be deleted after clicking the button

Guess you like

Origin blog.csdn.net/qq_37270421/article/details/133255703