Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WordCamp DC JavaScript Workshop

WordCamp DC JavaScript Workshop

These are the slides for my Introduction to JavaScript Workshop for WordCamp DC

Zac Gordon

July 16, 2017
Tweet

More Decks by Zac Gordon

Other Decks in Technology

Transcript

  1. WordCamp DC 2017 HTML Markup language using tags @zgordon CSS

    Presentation language using key value pairs JavaScript An actual programming language
  2. WordCamp DC 2017 It’s not that JavaScript is a hard

    or weird language. However, almost everything you do in JavaScript involves an API. That makes things more complicated. @zgordon
  3. WordCamp DC 2017 Web APIs Standards made available to allow

    JavaScript (ECMAScript) to interact with the browser (and more). W3C and WHATWG standards bodies. @zgordon
  4. WordCamp DC 2017 JavaScript & APIs Made Simple 1. JS

    Basics @zgordon 2. The DOM 3. DOM Events
  5. WordCamp DC 2017 How Did You Do??? POP QUIZ @zgordon

    +1 Point for Answer +1 Point for Code Example
  6. WordCamp Europe 2017 Variable A container for storing values (in

    memory) @zgordon var username = 'zgordon'; let username = 'zgordon'; const siteURL = 'https://site.com';
  7. WordCamp DC 2017 Array A collection of values @zgordon let

    postIds = [ 1, 2, 3, 5 ]; let usernames = [ 'zgordon', 'admin' ];
  8. WordCamp DC 2017 Array A collection of values (zero indexed)

    @zgordon let postIds = [ 1, 2, 3, 5 ]; postIds[ 0 ] // Equals 1 postIds[ postIds.length - 1 ] // Last item
  9. WordCamp DC 2017 Functions Let us write, call and reuse

    blocks of code @zgordon function getPosts() { let posts = apiMagic(); // Will learn return posts; } getPosts();
  10. WordCamp Europe 2017 Function Parameters Can pass data into functions

    @zgordon function getUser( id = 0 ) { let user = apiMagic( id ); return user; } getUser( 1 );
  11. WordCamp DC 2017 Objects Container with properties (values) and methods

    (functions). @zgordon let post = { 'title more': 'Hello World!', render: function() { console.log( this.title ); } } post.title; post.render();
  12. WordCamp DC 2017 Loops Let us perform an action on

    a collection of items. @zgordon let postIds = [ 1, 7, 14, 34, 88, 117 ]; for ( let i = 0, max = postIds.length; i < max; i++ ) { console.log( 'Post #' + postIds[ i ] ); }
  13. WordCamp DC 2017 For Of Loop Lets us loop through

    an array @zgordon var postIds = [ 1, 7, 14 ]; for( let id of postIds ) { console.log( id ); }
  14. WordCamp DC 2017 Conditional Statements Tests to determine what code

    to run @zgordon let loggedIn = true; if ( true === loggedIn ) { console.log( 'Show dashboard' ); } else { console.log( 'Please login' ); }
  15. WordCamp DC 2017 Include JS in HTML @zgordon <html> <head>

    </head> <body> <script src="index.js"></script> </body> </html>
  16. WordCamp DC 2017 Include JS in WP Theme function theme_scripts()

    { wp_enqueue_script( 'main-js', get_template_directory_uri() . '/js/main.js', [ 'jquery' ], time(), true ); } add_action('wp_enqueue_scripts', 'theme_scripts', 999); @zgordon
  17. WordCamp DC 2017 JavaScript & APIs Made Simple 1. JS

    Basics @zgordon 2. The DOM 3. DOM Events
  18. WordCamp DC 2017 Build documents, navigate their structure, and add,

    modify, or delete elements and content. @zgordon
  19. The DOM API 1. Build 2. Navigate 3. Add, Modify,

    Delete WordCamp DC 2017 @zgordon
  20. The DOM API 1. Build Nodes 2. Navigate Selection, Traversal

    3. Add, Modify, Delete Append, Set, Remove WordCamp DC 2017 @zgordon
  21. WordCamp DC 2017 Common Node Types • Document [9] •

    DocumentType [10] • Element [1] • Text [3] • Comments [8] • DocumentFragments [11] @zgordon
  22. WordCamp DC 2017 Common Node Types • Document [9] index.html

    • DocumentType [10] • Element [1] • Text [3] • Comments [8] • DocumentFragments [11] @zgordon
  23. WordCamp DC 2017 10 Doctype Root / HTML 1 Body

    1 Head 1 1 Title 3 Title Text 1 Header 1 Paragraph 3 Header Text 3 Text 1 Link 3 3 Link Text 9 Document @zgordon Window
  24. WordCamp Miami 2017 Getting HTML Elements document.getElementById( 'main' ) document.getElementsByTagName(

    'a' ) document.getElementsByClassName( 'post' ) document.querySelector( '.entry-title a' ) document.querySelectorAll( '.entry-title a' ) @zgordon WordCamp DC 2017
  25. WordCamp Miami 2017 Getting Node Values el.innerText el.innerHTML input.value @zgordon

    el.id el.href el.dataset.custom const el = document.getElementById( 'main' ); WordCamp DC 2017
  26. WordCamp Miami 2017 Setting Node Values el.innerText = 'New'; el.innerHTML

    = '<p>New</p>'; input.value = 'zgordon'; @zgordon const el = document.getElementById( 'main' ); WordCamp DC 2017
  27. WordCamp Miami 2017 Styling Nodes @zgordon window.getComputedStyle( el ) el.style.backgroundColor

    = '#f7b733' el.classList.add( 'new' ) el.classList.remove( 'new' ) el.classList.toggle( 'new' ) const el = document.getElementById( 'main' ); WordCamp DC 2017
  28. WordCamp Miami 2017 10 Doctype Root / HTML 1 Body

    1 Head 1 1 Title 3 Title Text 1 Header 1 Paragraph 3 Header Text 3 Text 1 Link 3 3 Link Text 9 Document @zgordon Window WordCamp DC 2017
  29. WordCamp Miami 2017 @zgordon p div Text p a Text

    Text Text Sibling WordCamp DC 2017
  30. WordCamp Miami 2017 @zgordon p div Text p a Text

    Text Text Child Text WordCamp DC 2017
  31. WordCamp Miami 2017 @zgordon div Text p a Text Text

    Text Children Text Text p WordCamp DC 2017
  32. WordCamp Miami 2017 DOM Traversal @zgordon Any Node el.parentNode el.childNodes

    el.firstChild el.lastChild el.nextSibling el.previousSibling Element Nodes el.parentElement el.children el.firstElementChild el.lastElementChild el.nextElementSibling el.previousElementSibling WordCamp DC 2017
  33. WordCamp Miami 2017 Creating Nodes document.createElement( 'p' ) document.createTextNode( 'Text'

    ) document.createDocumentFragment() @zgordon WordCamp DC 2017
  34. WordCamp Miami 2017 Appending Nodes parent.appendChild( newEl ) parent.insertBefore( newEl,

    el ) parent.innerHTML = '<p>Text</p>' @zgordon WordCamp DC 2017
  35. WordCamp Miami 2017 Appending Nodes let pEl = document.querySelector( 'p'

    ), aEl = document.createElement( 'a' ), aText = document.createTextNode( 'Link' ); aEl.appendChild( aText ); aEl.href = 'https://javascriptforwp.com'; pEl.appendChild( aEl ); @zgordon WordCamp DC 2017
  36. WordCamp Miami 2017 “Fake” Appending Nodes let pEl = document.querySelector(

    'p' ), markup = ''; markup += '<a href="https://site.com">' ); markup += 'Link Text'; markup += '</a>'; pEl.innerHTML = markup; @zgordon WordCamp DC 2017
  37. WordCamp Miami 2017 Cloning Nodes el.cloneNode( true/false ) @zgordon Removing

    Nodes el.remove() el.removeChild() WordCamp DC 2017
  38. WordCamp Miami 2017 Cloning and Removing let ul = document.querySelector(

    'ul' ), li = ul.firstElementChild.cloneNode( true ); ul.appendChild( li ); ul.firstElementChild.remove(); @zgordon WordCamp DC 2017
  39. WordCamp Miami 2017 JavaScript & APIs Made Simple 1. JS

    Basics @zgordon 2. The DOM 3. DOM Events WordCamp DC 2017
  40. WordCamp Miami 2017 Types of Events @zgordon • Mouse events

    • Keyboard events • Form events • Media events • Drag and Drop events • Window events • Many more…. https://javascriptforwp.com/intro-to-events/ WordCamp DC 2017
  41. WordCamp Miami 2017 Hooking into Events w JavaScript @zgordon 1.

    Inline in HTML 2. Global 1 Off 3. Listeners Best <a onclick="alert('hi')">Alert</a> a.onclick = sayHi a.addEventListener( 'click’, sayHi, false ) WordCamp DC 2017
  42. WordCamp Miami 2017 Event Listeners const linkEl = document.querySelector( 'a'

    ); function displayLinkInfo( event ) { event.preventDefault(); console.log( event.target.innerHTML ); } linkEl.addEventListener( 'click', displayLinkInfo, false ); @zgordon WordCamp DC 2017
  43. WordCamp Miami 2017 Removing Event Listeners linkEl.addEventListener( 'click', func, false

    ); linkEl.removeEventListener( 'click', func, false ); linkEl.addEventListener( 'click', function( event ) { console.log( event ); }, false ); @zgordon WordCamp DC 2017
  44. WordCamp Miami 2017 Event Listeners link.addEventListener( 'click', sayHi, false );

    function sayHi( event ) { // event is the event object console.log( event ); console.log( event.type ); console.log( event.target ); } @zgordon WordCamp DC 2017
  45. WordCamp Miami 2017 Event Propagation How notifications of events spreads

    through the DOM @zgordon javascriptforwp.com/event-propagation WordCamp DC 2017
  46. WordCamp Miami 2017 @zgordon Event Propagation <html> <body> <p> <a>

    document window Phase 1 - Capturing WordCamp DC 2017
  47. WordCamp Miami 2017 @zgordon Event Propagation <html> <body> <p> <a>

    document window Phase 2 - Target <a> Phase 1 - Capturing WordCamp DC 2017
  48. WordCamp Miami 2017 @zgordon Event Propagation <html> <body> <p> <a>

    document window <a> Phase 1 - Capturing Phase 2 - Target Phase 3 - Bubbling WordCamp DC 2017
  49. WordCamp Miami 2017 @zgordon Event Propagation <html> <body> <p> <a>

    document window Phase 1 - Capturing Phase 2 - Target Phase 3 - Bubbling <a> WordCamp DC 2017
  50. WordCamp Miami 2017 Event Propagation Bubbling el.addEventListener( 'click', sayHi, false

    ); Capturing el.addEventListener( 'click', sayHi, true ); Stopping Propagation event.stopPropagation(); @zgordon WordCamp DC 2017
  51. WordCamp Miami 2017 JavaScript & APIs Made Simple 1. JS

    Basics @zgordon 2. The DOM 3. DOM Events WordCamp DC 2017
  52. WordCamp DC 2017 JavaScript & APIs Made Simple 1. JS

    Basics @zgordon 2. The DOM 3. DOM Events
  53. WordCamp Miami 2017 It’s not that JavaScript is a hard

    or weird language. However, almost everything you do in JavaScript involves an API. That makes things more complicated. @zgordon WordCamp DC 2017