Java Script Introduction
Java Script Introduction
This example uses the method to "find" an HTML element (with id="demo") and
changes the element content (innerHTML) to "Hello JavaScript":
Program Example :
<html>
<body>
</body>
</html>
Output :
Click Me!
JavaScript Can Change HTML Attribute Values
In this example JavaScript changes the value of the src (source) attribute of an
<img> tag:
Program Example :
<!DOCTYPE html>
<html>
<body>
<p>In this case JavaScript changes the value of the src (source) attribute of an image.</p>
</body>
</html>
Output:
In this case JavaScript changes the value of the src (source) attribute of an image.
Example
document.getElementById("demo").style.fontSize = "35px";
or
document.getElementById('demo').style.fontSize = '35px';
Example
document.getElementById("demo").style.display = "none";
or
document.getElementById('demo').style.display = 'none';
Example
document.getElementById("demo").style.display = "block";
or
document.getElementById('demo').style.display = 'block';
JavaScript Where To
The <script> Tag
In HTML, JavaScript code must be inserted between <script> and </script>
tags.
Example
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
For example, a function can be called when an event occurs, like when the user
clicks a button.
JavaScript in <head>
In this example, a JavaScript function is placed in the <head> section of an
HTML page.
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
</body>
</html>
External JavaScript
Scripts can also be placed in external files:
To add several script files to one page - use several script tags:
Example
<script src="myScript1.js"></script>
<script src="myScript2.js"></script>