The script tag allows us to add JavaScript code into an html file.
Allow us to add buttons to a web page:
We can add events to buttons using onclick
. onclick
is set to a function, which will execute when the button is clicked:
Position determines whether an element is able to move from its current location.
The styles left
, right
, top
, and bottom
can be used to reorient an element once their position has been changed. left
moves an object from the left side of the container, right
moves an object from the right side, top
moves an object from the top, and bottom
moves an object from the bottom.
Forms can be written using the <form></form>
tag.
Forms are filled with <input>
tags, which allow users to input data.
Forms make use of the attribute onsubmit
, which executes a function when the form has been submitted. For example, <form onsubmit="myFunction()"></form>
will execute the function myFunction
when the form has been submitted. Forms normally submit data to a server, but to prevent that, use .preventDefault()
. This will stop the form from submitting the data, and allow the webpage to remain static.
<input>
tag has several attributes:
type
- this determines the type of input expected of the user.
name
- this gives the input a name. This is valuable when using serializeArray
, as the name of the input is directly associated with the value
of the input.value
- if an input does not take a value, then it's important to set a value so the input is associated with a specific value when using serializeArray
. For example, <input type="radio" value="true">
will associate the value true
with that radio input
.Prints values to the console. Accessed by going to the browsers development tool.
prompt()
is a function that allows users to enter input.
alert()
provides a pop-up to users.
The Document Object Model (DOM) makes HTML elements mutable using other programming languages.
Here are some useful functions to access HTML elements:
Create new elements:
Add elements to HTML file:
Styling an element:
Add an event attribute to an element:
this
selects the element or object that is calling a function. this can be passed as a parameter in a function, or used within one.
Keyboard events can be triggered using the keywords keydown
and keyup
.
Animations are a gradual change in an element's style that occurs over time. We can create animations by using setInterval
and clearInterval
:
jQuery is a JavaScript library that makes DOM traversals more manageable.
To add jQuery to our web pages, we need this link:
To select and element in the DOM, use the keyword $
.
jQuery comes with it's own functions as well:
Iterating through elements with jQuery
serializeArray
returns form data as a list of data objects. Each data object has two properties - name
and value
. For example:
When $("form").serializeArray()
is called, the function will return a list of data objects:
Where "Text Value" represents the text input by the user.
The .getJSON
function returns a JSON object from a URL.
We can store data locally in our browser using Local Storage.
We can store values in Local Storage using a key, value notation:
Storing lists and objects in Local Storage requires the use of JSON to "stringify" the values:
Objects are an important data structure that store data in the form of properties and values.