Why JavaScript ?
This page contains some examples of what JavaScript can do.
* JavaScript Can Change HTML Content
One of many JavaScript HTML methods is getElementById()
.
The example below "finds" an HTML element (with id="demo"), and changes the element content (innerHTML) to "Hello JavaScript":
Example
document.getElementById("demo").innerHTML = "Hello JavaScript";
JavaScript accepts both double and single quotes:
Example
* JavaScript Can Change HTML Attribute Values
In this example, JavaScript changes the value of the src
(source) attribute of an <img>
tag:
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p>JavaScript can change HTML attribute values.</p>
<p>In this case JavaScript changes the value of the src (source) attribute of an image.</p>
<button onclick="document.getElementById('myImage').src='new_image.png'">Click to change image to ("new_image.png")</button>
<img id="myImage" src="old_image.png" style="width:100px">
<button onclick="document.getElementById('myImage').src='old_image.png'">Click to change image to ("old_image.png")</button>
</body>
</html>
* JavaScript Can Change HTML Styles (CSS)
Changing the style of an HTML element, is a variant of changing an HTML attribute:
Example
document.getElementById("demo").style.fontSize = "35px";
* JavaScript Can Hide HTML Elements
Hiding HTML elements can be done by changing the display
style:
Example
document.getElementById("demo").style.display = "none";
* JavaScript Can Show HTML Elements
Showing hidden HTML elements can also be done by changing the display
style:
Example
Did You Know?
JavaScript and Java are completely different languages, both in concept and design.
JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in 1997.
ECMA-262 is the official name of the standard. ECMAScript is the official name of the language.