SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
Introduction to
JavaScript #3
@danielknell
Friday, 11 October 13
Recap
var element = document.getElementById('puzzle');
var elements = document.getElementsByTagName('div');
var elements = document.getElementsByClassName('submit'); // not IE < 9
var element = document.querySelector('#reload .submit'); // not IE < 8
var elements = document.querySelectorAll('#reload .submit'); // not IE < 8
element.getElementById('child');
element.getElementsByTagName('span');
element.getElementsByClassName('foo');
element.querySelector('#reload .submit'); // not IE < 8
element.querySelectorAll('#reload .submit'); // not IE < 8a
Friday, 11 October 13
Recap
var children = element.children;
var parent = element.parentNode;
Friday, 11 October 13
Recap
var parent = document.getElementById('foobar');
var element = document.createElement('div');
parent.appendChild(element);
parent.insertBefore(element, someOtherElement);
parent.removeChild(element);
element.parentNode.removeChild(element);
element.textContent = 'hello world';
Friday, 11 October 13
Recap
var ol = document.createElement('ol')
, el
;
document.getElementById('output').appendChild(ol);
for (var i = 1; i <= 100; ++i) {
el = document.createElement('li');
if (i % 3 === 0 && i % 5 === 0) {
el.textContent = 'FizzBuzz';
}
else if (i % 3 === 0) {
el.textContent = 'Fizz';
}
else if (i % 5 === 0) {
el.textContent = 'Buzz';
}
else {
el.textContent = i;
}
ol.appendChild(el);
}
Friday, 11 October 13
Recap
var el = document.getElementById('output');
el.style.backgroundImage = "url('bg.jpg')";
el.style.fontWeight = ‘bold’;
el.style.color = ‘red’;
window.getComputedStyle(el).backgroundImage; // not IE <
9
el.currentStyle.backgroundImage; // IE < 9
Friday, 11 October 13
Recap
var items = ol.children
;
for (var i = 0; i < items.length; ++i) {
el = items[i];
if (
el.textContent === 'FizzBuzz' ||
el.textContent === 'Fizz' ||
el.textContent === 'Buzz'
) {
el.style.fontWeight = 'bold';
}
if (i % 2 === 0) {
el.style.color = 'red';
}
}
Friday, 11 October 13
http://artsn.co/js-tpl
Friday, 11 October 13
Nested Loops
var data = [[0, 1, 2, 3], [4, 5], [6, 7, 8, 9]]
;
for (var x = 0; x < data.length; ++x) {
for (var y = 0; y < data[x].length; ++y) {
console.log(data[x][y]);
}
}
Friday, 11 October 13
Element Property
var el = document.getElementById('output');
el.id;
el.className;
Friday, 11 October 13
Math is Hard
Friday, 11 October 13
Math is Hard
.item {
display: block;
position: absolute;
line-height: 40px;
text-align: center;
}
#output {
position: relative;
overflow: hidden;
border: 1px solid #000;
margin: 20px auto;
}
Friday, 11 October 13
Math is Hard
var x
, y
, i = 0
, pieceWidth = 40
, pieceHeight = 40
, el = document.getElementById('output')
, size = 11
;
for (x = 0; x < size; x++) {
for (y = 0; y < size; y++) {
child = document.createElement('div');
child.style.width = pieceWidth + 'px';
child.style.height = pieceHeight + 'px';
child.style.left = (x * pieceWidth) + 'px';
child.style.top = (y * pieceHeight) + 'px';
child.className = 'item'
// to be continued...
Friday, 11 October 13
Math is Hard
if (x === 0 || y === 0) {
child.style.backgroundColor = '#eee';
}
if (x === 0 && y === 0) {
child.innerHTML = '+'
}
else {
child.textContent = x + y;
}
el.appendChild(child);
i++;
}
}
el.style.width = pieceWidth * size + 'px';
el.style.height = pieceHeight * size + 'px';
Friday, 11 October 13
Element Attribute
var el = document.getElementById('output');
el.setAttribute('lang', 'en');
el.getAttribute('lang');
// <div id="output" lang="en"></div>
el.setAttribute('data-foo', 'foo');
el.getAttribute('data-foo');
// <div id="output" data-foo="foo"></div>
Friday, 11 October 13
Slide Puzzle
Friday, 11 October 13
Slide Puzzle
var x
, y
, i = 0
, bgX
, bgY
, size = 5
, pieceWidth = Math.floor(612 / size)
, pieceHeight = Math.floor(612 / size)
, el = document.getElementById('output')
;
for (x = 0; x < size; x++) {
for (y = 0; y < size; y++) {
if (x == size - 1 && y == size - 1) {
continue;
}
bgX = pieceWidth * x;
bgY = pieceHeight * y;
child = document.createElement('div');
// to be continued...
Friday, 11 October 13
Slide Puzzle
child.style.backgroundPosition = "-" + bgX + 'px -' + bgY
+ 'px';
child.style.width = pieceWidth + 'px';
child.style.height = pieceHeight + 'px';
child.style.left = (x * pieceWidth) + 'px';
child.style.top = (y * pieceHeight) + 'px';
child.className = 'piece'
child.setAttribute('data-x', x);
child.setAttribute('data-y', y);
el.appendChild(child);
}
}
el.style.width = pieceWidth * size + 'px';
el.style.height = pieceHeight * size + 'px';
Friday, 11 October 13
Events
Friday, 11 October 13
Events
var el = document.getElementById('output');
el.addEventListener('click', callback, false); // not IE < 9
el.attachEvent('onclick', callback); // IE < 9
Friday, 11 October 13
Events
var el = document.getElementById('output');
function callback(e) {
alert('hello world');
e.preventDefault();
}
el.addEventListener('click', callback, false);
Friday, 11 October 13
Greeter
Friday, 11 October 13
Greeter
<div id="output">
<form id="form">
<label for="input">Name</label>
<input id="input" type="text">
<button type="submit">Greet</button>
</form>
</div>
Friday, 11 October 13
Moar Math!
Friday, 11 October 13
Moar Math!
Math.round(0.5); // 1
Math.floor(0.9); // 0
Math.ceil(0.1); // 1
Math.abs(-1); // 1
Math.sqrt(9); // 3
Math.sin(1); // 0.8414709848078965
Math.cos(1); // 0.5403023058681398
Math.tan(1); // 1.5574077246549023
Math.asin(1); // 1.5707963267948966
Math.acos(1); // 0
Math.atan(1); // 0.7853981633974483
Math.min(1, 5); // 1
Math.max(1, 5); // 5
Math.PI; // 3.141592653589793
Math.E; // 2.718281828459045
Friday, 11 October 13
Slide Puzzle
.piece {
background-image: url('../img/image.jpg');
display: block;
position: absolute;
transition: top 1s, left 1s;
}
#output {
position: relative;
overflow: hidden;
border: 1px solid #000;
margin: 20px auto;
}
Friday, 11 October 13
Slide Puzzle
Friday, 11 October 13
Thats All Folks
email: contact@danielknell.co.uk
twitter: @danielknell
website: http://danielknell.co.uk/
Friday, 11 October 13

Contenu connexe

Tendances

Kick start with j query
Kick start with j queryKick start with j query
Kick start with j query
Md. Ziaul Haq
 
Full compile invalid obje pl/sql
Full compile invalid obje pl/sqlFull compile invalid obje pl/sql
Full compile invalid obje pl/sql
Anar Godjaev
 

Tendances (7)

Events - Part 2
Events - Part 2Events - Part 2
Events - Part 2
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
Kick start with j query
Kick start with j queryKick start with j query
Kick start with j query
 
An Introduction to Jquery
An Introduction to JqueryAn Introduction to Jquery
An Introduction to Jquery
 
BVJS
BVJSBVJS
BVJS
 
Full compile invalid obje pl/sql
Full compile invalid obje pl/sqlFull compile invalid obje pl/sql
Full compile invalid obje pl/sql
 
Practical Event Sourcing
Practical Event SourcingPractical Event Sourcing
Practical Event Sourcing
 

En vedette

An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4
Event Handler
 
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup EmpireThe Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
Seh Hui Leong
 
The big bang theory - UNIT 2
The big bang theory - UNIT 2The big bang theory - UNIT 2
The big bang theory - UNIT 2
lm092068
 
Introduction to JavaScript: Week Two
Introduction to JavaScript: Week TwoIntroduction to JavaScript: Week Two
Introduction to JavaScript: Week Two
Event Handler
 
An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5
Event Handler
 
An Introduction to JavaScript: Week One
An Introduction to JavaScript: Week OneAn Introduction to JavaScript: Week One
An Introduction to JavaScript: Week One
Event Handler
 
Big Bang Theorychandler
Big Bang TheorychandlerBig Bang Theorychandler
Big Bang Theorychandler
guest008d7bd
 

En vedette (20)

An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4An Introduction to JavaScript: Week 4
An Introduction to JavaScript: Week 4
 
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup EmpireThe Big Bang Theory: Nine Steps To Building Your Meetup Empire
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
 
The big bang theory - UNIT 2
The big bang theory - UNIT 2The big bang theory - UNIT 2
The big bang theory - UNIT 2
 
Java Script
Java ScriptJava Script
Java Script
 
The big bang theory of social recruiting
The big bang theory of social recruitingThe big bang theory of social recruiting
The big bang theory of social recruiting
 
Introduction to JavaScript: Week Two
Introduction to JavaScript: Week TwoIntroduction to JavaScript: Week Two
Introduction to JavaScript: Week Two
 
8. java script
8. java script8. java script
8. java script
 
JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 
An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5An Introduction to JavaScript: Week 5
An Introduction to JavaScript: Week 5
 
Introduction to Java Script
Introduction to Java ScriptIntroduction to Java Script
Introduction to Java Script
 
Unchallengeable miracle of Holy Quran
Unchallengeable miracle of  Holy QuranUnchallengeable miracle of  Holy Quran
Unchallengeable miracle of Holy Quran
 
An Introduction to JavaScript: Week One
An Introduction to JavaScript: Week OneAn Introduction to JavaScript: Week One
An Introduction to JavaScript: Week One
 
Big Bang Theory
Big Bang TheoryBig Bang Theory
Big Bang Theory
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
Chapter 1 - How the world begin
Chapter 1 - How the world beginChapter 1 - How the world begin
Chapter 1 - How the world begin
 
Big Bang Theorychandler
Big Bang TheorychandlerBig Bang Theorychandler
Big Bang Theorychandler
 
Large-Scale JavaScript Development
Large-Scale JavaScript DevelopmentLarge-Scale JavaScript Development
Large-Scale JavaScript Development
 
Qur’an and its sciences
Qur’an and its sciencesQur’an and its sciences
Qur’an and its sciences
 
Java script Learn Easy
Java script Learn Easy Java script Learn Easy
Java script Learn Easy
 
The Quran and Computational Linguistics
The Quran and Computational LinguisticsThe Quran and Computational Linguistics
The Quran and Computational Linguistics
 

Similaire à An Introduction to JavaScript: Week 3

Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
jhchabran
 
HTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - AltranHTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - Altran
Robert Nyman
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
smueller_sandsmedia
 
[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx
gerardkortney
 
Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The LibrariesRediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
 

Similaire à An Introduction to JavaScript: Week 3 (20)

jQuery
jQueryjQuery
jQuery
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journey
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
06 jQuery #burningkeyboards
06 jQuery  #burningkeyboards06 jQuery  #burningkeyboards
06 jQuery #burningkeyboards
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
HTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - AltranHTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - Altran
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and Performance
 
ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014
 
Web Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for PerformanceWeb Optimization Summit: Coding for Performance
Web Optimization Summit: Coding for Performance
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
Javascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionJavascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introduction
 
Node.js - Demnächst auf einem Server in Ihrer Nähe
Node.js - Demnächst auf einem Server in Ihrer NäheNode.js - Demnächst auf einem Server in Ihrer Nähe
Node.js - Demnächst auf einem Server in Ihrer Nähe
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx[removed] $file, removeRemove}, list #su.docx
[removed] $file, removeRemove}, list #su.docx
 
JavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptJavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScript
 
Intro to Advanced JavaScript
Intro to Advanced JavaScriptIntro to Advanced JavaScript
Intro to Advanced JavaScript
 
Maintainable JavaScript 2011
Maintainable JavaScript 2011Maintainable JavaScript 2011
Maintainable JavaScript 2011
 
Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The LibrariesRediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The Libraries
 
JQuery
JQueryJQuery
JQuery
 
J Query
J QueryJ Query
J Query
 

Plus de Event Handler (8)

Selling UCD: Getting buy-in and measuring the value of UX
Selling UCD: Getting buy-in and measuring the value of UXSelling UCD: Getting buy-in and measuring the value of UX
Selling UCD: Getting buy-in and measuring the value of UX
 
Best Practice for UX Deliverables - 2014
Best Practice for UX Deliverables - 2014Best Practice for UX Deliverables - 2014
Best Practice for UX Deliverables - 2014
 
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip HopFrom Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
 
Tumours and Tree Trunks - GeekyScience: Evolution
Tumours and Tree Trunks - GeekyScience: EvolutionTumours and Tree Trunks - GeekyScience: Evolution
Tumours and Tree Trunks - GeekyScience: Evolution
 
Best Practice for UX Deliverables
Best Practice for UX DeliverablesBest Practice for UX Deliverables
Best Practice for UX Deliverables
 
The Missing Ingredient
The Missing IngredientThe Missing Ingredient
The Missing Ingredient
 
Productivity quickly
Productivity quicklyProductivity quickly
Productivity quickly
 
Anna pickard geekyfinal
Anna pickard geekyfinalAnna pickard geekyfinal
Anna pickard geekyfinal
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 

An Introduction to JavaScript: Week 3

  • 2. Recap var element = document.getElementById('puzzle'); var elements = document.getElementsByTagName('div'); var elements = document.getElementsByClassName('submit'); // not IE < 9 var element = document.querySelector('#reload .submit'); // not IE < 8 var elements = document.querySelectorAll('#reload .submit'); // not IE < 8 element.getElementById('child'); element.getElementsByTagName('span'); element.getElementsByClassName('foo'); element.querySelector('#reload .submit'); // not IE < 8 element.querySelectorAll('#reload .submit'); // not IE < 8a Friday, 11 October 13
  • 3. Recap var children = element.children; var parent = element.parentNode; Friday, 11 October 13
  • 4. Recap var parent = document.getElementById('foobar'); var element = document.createElement('div'); parent.appendChild(element); parent.insertBefore(element, someOtherElement); parent.removeChild(element); element.parentNode.removeChild(element); element.textContent = 'hello world'; Friday, 11 October 13
  • 5. Recap var ol = document.createElement('ol') , el ; document.getElementById('output').appendChild(ol); for (var i = 1; i <= 100; ++i) { el = document.createElement('li'); if (i % 3 === 0 && i % 5 === 0) { el.textContent = 'FizzBuzz'; } else if (i % 3 === 0) { el.textContent = 'Fizz'; } else if (i % 5 === 0) { el.textContent = 'Buzz'; } else { el.textContent = i; } ol.appendChild(el); } Friday, 11 October 13
  • 6. Recap var el = document.getElementById('output'); el.style.backgroundImage = "url('bg.jpg')"; el.style.fontWeight = ‘bold’; el.style.color = ‘red’; window.getComputedStyle(el).backgroundImage; // not IE < 9 el.currentStyle.backgroundImage; // IE < 9 Friday, 11 October 13
  • 7. Recap var items = ol.children ; for (var i = 0; i < items.length; ++i) { el = items[i]; if ( el.textContent === 'FizzBuzz' || el.textContent === 'Fizz' || el.textContent === 'Buzz' ) { el.style.fontWeight = 'bold'; } if (i % 2 === 0) { el.style.color = 'red'; } } Friday, 11 October 13
  • 9. Nested Loops var data = [[0, 1, 2, 3], [4, 5], [6, 7, 8, 9]] ; for (var x = 0; x < data.length; ++x) { for (var y = 0; y < data[x].length; ++y) { console.log(data[x][y]); } } Friday, 11 October 13
  • 10. Element Property var el = document.getElementById('output'); el.id; el.className; Friday, 11 October 13
  • 11. Math is Hard Friday, 11 October 13
  • 12. Math is Hard .item { display: block; position: absolute; line-height: 40px; text-align: center; } #output { position: relative; overflow: hidden; border: 1px solid #000; margin: 20px auto; } Friday, 11 October 13
  • 13. Math is Hard var x , y , i = 0 , pieceWidth = 40 , pieceHeight = 40 , el = document.getElementById('output') , size = 11 ; for (x = 0; x < size; x++) { for (y = 0; y < size; y++) { child = document.createElement('div'); child.style.width = pieceWidth + 'px'; child.style.height = pieceHeight + 'px'; child.style.left = (x * pieceWidth) + 'px'; child.style.top = (y * pieceHeight) + 'px'; child.className = 'item' // to be continued... Friday, 11 October 13
  • 14. Math is Hard if (x === 0 || y === 0) { child.style.backgroundColor = '#eee'; } if (x === 0 && y === 0) { child.innerHTML = '+' } else { child.textContent = x + y; } el.appendChild(child); i++; } } el.style.width = pieceWidth * size + 'px'; el.style.height = pieceHeight * size + 'px'; Friday, 11 October 13
  • 15. Element Attribute var el = document.getElementById('output'); el.setAttribute('lang', 'en'); el.getAttribute('lang'); // <div id="output" lang="en"></div> el.setAttribute('data-foo', 'foo'); el.getAttribute('data-foo'); // <div id="output" data-foo="foo"></div> Friday, 11 October 13
  • 17. Slide Puzzle var x , y , i = 0 , bgX , bgY , size = 5 , pieceWidth = Math.floor(612 / size) , pieceHeight = Math.floor(612 / size) , el = document.getElementById('output') ; for (x = 0; x < size; x++) { for (y = 0; y < size; y++) { if (x == size - 1 && y == size - 1) { continue; } bgX = pieceWidth * x; bgY = pieceHeight * y; child = document.createElement('div'); // to be continued... Friday, 11 October 13
  • 18. Slide Puzzle child.style.backgroundPosition = "-" + bgX + 'px -' + bgY + 'px'; child.style.width = pieceWidth + 'px'; child.style.height = pieceHeight + 'px'; child.style.left = (x * pieceWidth) + 'px'; child.style.top = (y * pieceHeight) + 'px'; child.className = 'piece' child.setAttribute('data-x', x); child.setAttribute('data-y', y); el.appendChild(child); } } el.style.width = pieceWidth * size + 'px'; el.style.height = pieceHeight * size + 'px'; Friday, 11 October 13
  • 20. Events var el = document.getElementById('output'); el.addEventListener('click', callback, false); // not IE < 9 el.attachEvent('onclick', callback); // IE < 9 Friday, 11 October 13
  • 21. Events var el = document.getElementById('output'); function callback(e) { alert('hello world'); e.preventDefault(); } el.addEventListener('click', callback, false); Friday, 11 October 13
  • 23. Greeter <div id="output"> <form id="form"> <label for="input">Name</label> <input id="input" type="text"> <button type="submit">Greet</button> </form> </div> Friday, 11 October 13
  • 24. Moar Math! Friday, 11 October 13
  • 25. Moar Math! Math.round(0.5); // 1 Math.floor(0.9); // 0 Math.ceil(0.1); // 1 Math.abs(-1); // 1 Math.sqrt(9); // 3 Math.sin(1); // 0.8414709848078965 Math.cos(1); // 0.5403023058681398 Math.tan(1); // 1.5574077246549023 Math.asin(1); // 1.5707963267948966 Math.acos(1); // 0 Math.atan(1); // 0.7853981633974483 Math.min(1, 5); // 1 Math.max(1, 5); // 5 Math.PI; // 3.141592653589793 Math.E; // 2.718281828459045 Friday, 11 October 13
  • 26. Slide Puzzle .piece { background-image: url('../img/image.jpg'); display: block; position: absolute; transition: top 1s, left 1s; } #output { position: relative; overflow: hidden; border: 1px solid #000; margin: 20px auto; } Friday, 11 October 13
  • 28. Thats All Folks email: contact@danielknell.co.uk twitter: @danielknell website: http://danielknell.co.uk/ Friday, 11 October 13