JavaScript By Example
Go back...
Getting started
JavaScript makes HTML pages more dynamic and interactive! Some common use cases for JavaScript include
image manipulation, form validation, and dynamic changes of content.
To start off, let's create our very first script! To do so, use the <script> HTML tag
to denote a script inside your HTML file. Make sure you don't forget to close the tag with a </script> closing tag.
Now, anything you write between these two tags will be interpreted by your browser as a piece of JavaScript code!
You can also include external scripts in your HTML file using the src attribute. It will be demonstrated in the examples below.
To select an HTML element, you can use the JavaScript document.getElementById() method. Don't worry, this will also be demonstrated
in the examples below!
Now buckle up, and get ready for some JavaScript action!
JavaScript Examples
|
<p id="hello"></p>
<script>
// This script edits the HTML element with an ID of "hello", to have the text "Hello, World!"
document.getElementById("hello").innerHTML = "Hello, World!";
</script>
|
|
<button onclick="doSomething()">Press me !!!!!</button>
|
|
<button id="coolbutton">Press me !!!!!</button>
|
Yeah, text can act as a "button" as well! I wish I could become RED one day... |
<p onclick="changeColor()">Yeah, text can act as a "button" as well!</p>
|
Here's something for the more adventurous. |
<p>Here's something for the more adventurous.</p>
|