JavaScript enhances HTML pages by making them more dynamic and interactive.
The HTML <script> Tag
The HTML <script>
tag is employed to define a client-side script (JavaScript).
The <script>
element contains script statements or references to an external script file via the src
attribute.
JavaScript is frequently used for tasks such as image manipulation, form validation, and dynamic content changes.
For selecting an HTML element, JavaScript primarily uses the document.getElementById()
method.
The following JavaScript example writes "This is JavaScript!" into an HTML element with the id of "myid"
.
<!DOCTYPE html>
<html>
<head>
<script>
document.getElementById("myid").innerHTML = "This is JavaScript";
</script>
</head>
<body>
<div id="myid"></div>
</body>
</html>
More JavaScript Examples
Below are some examples showcasing JavaScript's capabilities.
document.getElementById("myid").style.fontSize = "25px";
The above code will set the text size of "myid" to 25px;
document.getElementById("myid").style.color = "red";
The above code will set the text color of "myid" to red.
document.getElementById("myid").style.backgroundColor = "yellow";
The above code will set the background color of "myid" to yellow.
document.getElementById("image").src = "image.png";
The above code will set the src
attribute value of the "image" id to "image.png".
The HTML <noscript> Tag
The HTML <noscript>
tag specifies alternative content to display for users who have disabled scripts in their browser, or are using a browser that doesn't support scripts.
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
<noscript>Oops, your browser does not support JavaScript!</noscript>
You'll learn more about JavaScript in our JavaScript reference guide.
Please check below options for the links to our previous or next tutorial.