page heading

links:

notes on facts and random tags

strong
emphasis

harder things to code:

multiple 3 4
5 15 20
6 18 24

simple coding things:

thumb

list item
description of item
another item
another

JavaScript notes

html is (declerative) describes how things should be do this do that. code>compile>run>repeat
javascript is procedural describes step-by-step. Interpreted language code>run>repeat. weakly typed
primtive data type way of defining and grouping values that cant be changed
these are number,string,boolean,undefined,null,bigInt,symbol
non prmitive are array and object (immutuable) cant change but can reassign
/ is div % is modulo the thing to find remainders 0.5->1 so odd
console.log(typeof 42); returns number finds the type of data.
&& - AND || - OR ! - NOT
String.charAt() find where a letter is the index
String.indexOf() if the substring cant be found -1 is returned.
String.toLowerCase() String.length()
String.trim() removes white spaces
String.substr(index,length) returns part of a string starting at index
string.split(" ") breaks a string up into an array. this example is one word per index
string.startsWith() if the string typed in if it starts like that returns True. string.endsWith()
function multiply(x=1,y=2) {return x*y}; console.log(multiply(7,8)) default paramteres are 1 and 2
var has local/global scope and is hoisted. let has block scope and is not hoisted

array.unshift() - add elemts to start of an arry and returns value. opposite of push
array.shift() - removes first elements returns. opposite of pop()
array.join() - joins the things into a String
array.map() coding
array.filter() - creates new array with all elements that pass the test to be implemented by the provided function

week 10 and 11 DOM notes

DOM stands for Document Object Model
Lets JavaScript access, add, remove, or change things in the HTML and CSS
The DOM is a tree structure made from the HTML when the page loads
Everything in the HTML becomes a node (elements, text, attributes)

Ways to access elements:
document.getElementById("id") – gets one element with that ID
document.getElementsByClassName("class") – gets a list of elements with that class
document.getElementsByTagName("tag") – gets all elements of that type
document.querySelector("selector") – gets the first element that matches a CSS selector
document.querySelectorAll("selector") – gets all elements that match a CSS selector

querySelector uses CSS rules (use # for id and . for class)
Once you have an element, you can get or change its content with element.textContent
You can also get or set attributes with element.getAttribute() and element.setAttribute()

You can’t access the DOM until it’s fully loaded
The browser loads HTML line by line and runs JavaScript as it sees it
If JavaScript runs too early, it might not find the element yet

Two ways to fix this:
1. Use the load event:
window.addEventListener("load", function() {document.querySelector("#target");});
2. Put the script at the bottom of the HTML page before
An event = something that happens on the page (like a click or load)
Events are how JavaScript reacts to user actions or browser actions
Examples: click, load, mouseover, keydown, focus, blur
Events are tied to specific objects (like buttons, the document, or the window)

Event listeners wait for a certain event to happen, then run code
You register a listener using:
element.addEventListener("event", function)

Good practice: addEventListener (separates HTML and JS)
Bad practice: putting JS directly in HTML like onclick="..."

You can also remove listeners with removeEventListener

Inside an event handler, this refers to the element that caused the event
You can use this to get info about the element (like its value or href)

Every event gives you an event object (usually called event or e)
You can use it to get details about what happened or stop default behavior
Example: event.preventDefault() stops a link from navigating

Most JS code runs because of events (e.g. after the page loads, or when a button is clicked)
we can change how things look on the page using javascript
two ways to style elements:
1. use element.style.property = value
example: element.style.color = "red"
this changes the inline style in the html
it will override anything in the css file

2. use classList to add or remove css classes
example: element.classList.add("error")
better than using style because it keeps js and css separate
you can also use classList.remove("class") and classList.toggle("class")
classList.contains("class") checks if it has the class

computed style = what the browser actually applies after checking all css (inline, file, defaults)
use window.getComputedStyle(element) to read final styles
example: window.getComputedStyle(para).color

to create elements:
use document.createElement("tag")
use element.textContent = "text" to add text
use element.setAttribute("class", "classname") to set a class
use parent.appendChild(element) to add it to the page

don’t use innerHTML to create elements, it's messy and not safe

to remove elements:
parent.removeChild(child) removes one element
to remove all child elements:
let p = document.querySelector("#parent");
while(p.lastChild) { p.removeChild(p.lastChild); }
Working with forms – submitting, reading values, validating input
submit event used most, but also click, keyup, focus, blur etc
always validate: check fields filled, data is sensible, and safe
data can be malicious – XSS, SQL injection etc
after validation, data is used or sent to server and page updates

form has: id, name, action, method
id – identify element for JS or CSS
name – field name when sent to server (not for JS access)
action – server endpoint for form data
method – GET or POST for sending data
for – links label to input using id
type – kind of input (text, email, password etc)

Accessing form elements:
Best: document.getElementById() or document.querySelector()
Not recommended: document.forms[], document.login etc

Getting values:
text, email, password, textarea – use .value
select – use .value and .selectedIndex
checkbox – use .checked and .value
radio – use .checked and .value

Validation events:
keyup – live feedback like password strength
blur – check when field loses focus
change – for checkbox, radio, select
click – on buttons to do things
submit – last check before sending form (ENTER/RETURN triggers it)

Always do final validation on submit
Use MDN for more info on forms and validation

go to top