JavaScript is the most popular programming language in the world.
It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more.
This page contains some examples of what JavaScript can do in HTML.
JavaScript Can Change HTML Elements
The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements.
JavaScript can manipulate the DOM (change HTML contents).
The following example changes the content (innerHTML) of an HTML element identified with id="demo":
<!DOCTYPE html>
<html>
<body>
<h1>My First JavaScript</h1>
<p>JavaScript can change the content of an HTML element:</p>
<button type="button" onclick="myFunction()">Click Me!</button>
<p id="demo">This is a demonstration.</p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello JavaScript!";
}
</script>
</body>
</html>
Output:
My First JavaScript
JavaScript can change the content of an HTML element:
This is a demonstration.
Click to Click Me! button the outptut:
My First JavaScript
JavaScript can change the content of an HTML element:
Hello JavaScript!
The method document.getElementById() is one of many methods in the HTML DOM.
You can use JavaScript to:
- Change HTML elements
- Delete HTML elements
- Create new HTML elements
- Copy and clone HTML elements
- And much more ...
JavaScript Can Change HTML Attributes
This example changes the value of the source attribute (src) of an HTML <image> element:
Click the light bulb to turn on/off the light
No comments:
Post a Comment