Your Content
+ HTML: Structure
+ CSS: Presentation
= Your Website
A website is a way to present your content to the world, using HTML and CSS to present that content & make it look good.
Photo credit: Nic's events cc
Photo credit: pumpkincat210 cc
ID -- Should only apply to one element on a webpage, i.e., a webpage only has one footer.
The "#" is how you tell CSS "this is an id."
Class -- Lots of elements can have the same class, i.e., There can be many warnings on one webpage.
The "." is how you tell CSS "this is a class name."
HTML elements "nest" inside one another
We model the nested structure of an HTML page with the DOM (Document Object Model) Tree. The browser makes a "map" of all the elements on a page.
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<style>
h1 {
color: red;
}
</style>
</head>
<body>
<h1>My Page</h1>
<p>Hello World!</p>
<img src="http://placekitten.com/g/200/300" alt="cat"/>
</body>
</html>
Your browser automatically builds a Document object to store the DOM of a page. To change a page:
Photo credit: Jeffery Beall cc
You can find nodes by id using the method:
document.getElementById(id);
To find:
<img id="kittenPic" src="http://placekitten.com/g/200/300" alt="cat"/>
We would use:
var imgKitten=document.getElementById('kittenPic');
You can certain types of HTML elements using this method:
document.getElementsByTagName(tagName);
To find:
<li>Daisy</li>
<li>Tulip</li>
We would use:
var listItems=document.getElementsByTagName('li');
for (var i=0; i < listItems.length; i++) {
var listItem=listItems[i];
}
In newer browsers, you can use other methods too.
Available in IE9+, FF3.6+, Chrome 17+, Safari 5+:
document.getElementsByClassName(className);
Available in IE8+, FF3.6+, Chrome 17+, Safari 5+:
document.querySelector(cssQuery);
document.querySelectorAll(cssQuery);
Any method that starts with "getElement" will return a single node.
document.getElementById('uniqueID'); //returns a single node
Any method that starts with "getElements" will return a array of nodes. To modify a single node, you will need to use bracket notation to select the correct one.
document.getElementsByTagName('p'); //returns multiple nodes
var specficParagraph=document.getElementsByTagName('p')[2];
Photo credit: Darryl Kenyon cc
You can access and change attributes of DOM nodes using dot notation.
To change this element:
<img id="kittenPic" src="http://placekitten.com/g/200/300" alt="cat"/>
We could change the src attribute this way:
var imgKitten=document.getElementById('kittenPic');
var oldSrc=imgKitten.src;
imgKitten.src='http://placekitten.com/g/600/500';
You can also use getAttribute/setAttribute
<img id="kittenPic" src="http://placekitten.com/g/200/300" alt="cat"/>
We could change the src attribute this way:
var imgKitten=document.getElementById('kittenPic');
var oldSrc=imgKitten.getAttribute('src');
imgKitten.setAttribute('src', 'http://placekitten.com/g/600/500');
You can change page css usingstyle
To make this CSS:
body {
color: red;
}
Use this JavaScript:
var pageNode=document.getElementsByTagName('body')[0];
pageNode.style.color='red';
Change any CSS property with a "-" to camelCase, and be sure to include a unit on any number
To make this CSS:
body {
background-color: pink;
padding-top: 10px;
}
Use this JavaScript:
var pageNode=document.getElementsByTagName('body')[0]
pageNode.style.backgroundColor='pink';
pageNode.style.paddingTop='10px';
Create a simple HTML page or use this sample code.
Isolate a node (an element on the page) and change an attribute or add a new style.
Each DOM node has an innerHTML
property with the HTML of all its children. You can use the property to view or change the HTML of a node.
For example, you can overwrite the entire body:
var pageNode=document.getElementsByTagName('body')[0];
pageNode.innerHTML='<h1>Oh Noes!</h1>
<p>I just changed the whole page!</p>'
Or just add some new content to the end
pageNode.innerHTML +='...just adding this bit at the end of the page.';
You can also target a particular element
To fill this HTML element:
<p id="warning"></p>
We can select the node and modify it
var warningParagraph=document.getElementById('warning');
warningParagraph.innerHTML='Danger Will Robinson!';
The document
object also provides ways to create nodes from scratch:
document.createElement(tagName);
document.createTextNode(text);
document.appendChild();
var pageNode=document.getElementsByTagName('body')[0];
var newImg=document.createElement('img');
newImg.src='http://placekitten.com/g/500/200';
newImg.style.border='1px solid black';
pageNode.appendChild(newImg);
var newParagraph=document.createElement('p');
var paragraphText=document.createTextNode('Squee!');
newParagraph.appendChild(paragraphText);
pageNode.appendChild(newParagraph);
Create a new paragraph element and add it to a div on your page.
An 'event' is a type of object that is created when the user interacts with a web page.
For example, JS creates an event when a user clicks an element.
element.onclick=function () {
//code to execute when the click happens
};
There are variety of events. Some of the most common:
How do you make an event trigger some code?
You can call a function directly from your HTML code:
<button id="clickMe" onclick="sayHi()">Click Me!</button>
function sayHi() {
alert ('Hi!');
}
Photo credit: Simon James cc
Listening functions work like many of the other things we have done. First, find the object:
var myTarget=document.getElementById('clickMe');
Then add an event to that object:
myTarget.onclick=function(){
alert ('Hi!');
}
Go back to the sample files you downloaded earlier.
Make some JavaScript code fire after an onmouseover
event. Try adding the event to the HTML and adding a listening function.
Elements like buttons and links have a default behaviors. However, the event
objects has a built-in method to handle that:
element.onclick=function (event) {
event.preventDefault();
};
Photo credit: Alan Berning cc
The keyword this
references the element that the user has just interacted with
element.onmouseover=function (event) {
console.log(this);
};
element.onclick=function (event) {
this.style.backgroundColor='red';
this.innerHTML='I've been clicked!';
};
Write code that targets this link:
<a href="http://girldevelopit.com/" id="gdiLink">Girl Develop It</a>
When a user clicks the link, the page should display an error message instead of going to the Girl Develop It homepage.
Photo credit: Rym DeCoster cc
You can collect information from users to use in functions. The most common method is an HTML form
<form id="temperatureForm">
<label for="temp">Temperature:</label> <input type="text" id="temp" size="3"/> degrees in
<input type="radio" name="tempFormat" value="F" checked />Fahrenheit
<input type="radio" name="tempFormat" value="C" />Celsius <br />
<label for="submitButton"></label> <input id="tempSubmitButton" type="submit" value="Submit" />
</form>
You retrieve the values of form elements using the value
method.
var temperature=document.getElementById('temp').value;
console.log (temperature);
You can retrieve the value of a form at any time. Try onblur
(when a form element loses focus).
Radio buttons usually do not have IDs, so you will need to use an array:
var radios=document.getElementsByName('tempFormat');
for (var i=0, length=radios.length; i < length; i++) {
if (radios[i].checked) {
var radioButtonValue=radios[i].value;
// only one radio can be checked, so stop now
break;
}
}
If you are going to retrieve form values with the submit button, be sure to prevent the default action!
var submitButton=document.getElementById('tempSubmitButton');
submitButton.onclick=function () {
event.preventDefault();
var temperature=document.getElementById('temp').value;
console.log (temperature);
}
Collect a value from the input box on the page. Use it inside a function of some kind. For example, collect a number and multiply it by five or collect a name and display a greeting.