JavaScript Basics


Introduction

JavaScript is a programming language that allows us to manipulate the content of a web page.

The most basic example of a JavaScript method is the getElementById() method.

I can change this text to whatever I want!

In the example above, the paragraph text is actually different initially, but the script after it changes the text to "Hello, world!".

Let's try executing some JavaScript code with a button.
We can do this by using the onclick attribute.

NOTE: The onclick attribute is a global attribute, so it can be used on any HTML element, not just buttons.


Altering HTML Attributes

We can also alter the attributes of an HTML element using JavaScript.

  1. Item 1
  2. Item 2
  3. Item 3

Altering CSS Styles

We can also alter the CSS styles of an HTML element using JavaScript.

Click the button to change the background color of the page.

NOTE: In the above example, we use rgb values for blue, and a hex value for red. There are many different ways to declare a color value in CSS.