Create / Delete DOM Elements Handle Browser Events

Slides:



Advertisements
Similar presentations
The jQuery library. What is jQuery ? A javascript lightweight library Is very easy to use Is powerful Is cross-browser compatible Downloadable from jQuery.com,
Advertisements

FrontPage Express By John G. Summerville Ph.D.©, RN.
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
Intermediate Level Course. Text Format The text styles, bold, italics, underlining, superscript and subscript, can be easily added to selected text. Text.
20-Jun-15 JavaScript and HTML Simple Event Handling.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
DOM and Events Document Object Model (DOM) Events Handling in JavaScript Svetlin Nakov Technical Trainer Software University
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Controllers and Markup Controllers, $scope, Markup, Directives, Expressions, Binding, Filters, Validation SoftUni Team Technical Trainers Software University.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
>> More on CSS Selectors. Pre-requisite Open the file called sample.txt Copy the all the text from the file Open your browser and go to codepen.io Paste.
SASS & LESS Syntactically Awesome StyleSheets Dynamic StyleSheet Language Svetlin Nakov Technical Trainer Software University
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
JQuery Overview Unleash the Power of jQuery SoftUni Team Technical Trainers Software University
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
 Add one element at a time  If it doesn’t do what you want, › Delete it › DON’T just keep piling on more items.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Game Development with JS and Canvas 2D
Helpers, Data Validation
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Introduction to MVC SoftUni Team Introduction to MVC
Cookies, Sessions, Bootstrap
JavaScript and HTML Simple Event Handling 11-May-18.
Build a WordPress Site A Real Life Example: Create a Fully Functional WP Business Web Site from Scratch Building a WP Site SoftUni Team Technical Trainers.
Source Control Systems
Bootstrap 3 SoftUni Team Technical Trainers Software University
State Management Cookies, Sessions SoftUni Team State Management
Browser Routing Design Patterns in JS
CSS Layouts: Grouping Elements
jQuery Library, Selectors, DOM Manipulation, Events, Plugins
MVC Architecture. Routing
Creating React Components with JSX and Babel
Debugging and Troubleshooting Code
Entity Framework: Relations
Fast String Manipulation
Array and List Algorithms
MVC Architecture, Symfony Framework for PHP Web Apps
AJAX and jQuery AJAX AJAX Concepts, XMLHttpRequest, jQuery AJAX: $.ajax(), $.get(), $.post() jQuery AJAX XMLHttpRequest SoftUni Team Technical Trainers.
Introduction to JavaScript Events
Creating UI elements with Handlebars
Arrays and Multidimensional Arrays
WordPress Plugins Popular WP Plugins: Sliders, Forms, Contacts, SEO, Forum, Photo Gallery, e-Commerce WordPress Plugins SoftUni Team Technical Trainers.
Extending functionality using Collections
CSS Transitions and Animations
Inheritance and Prototypes
Directives & Forms Capturing User Input SoftUni Team
CSS Transitions and Animations
Iterators and Generators
JavaScript and HTML Simple Event Handling 19-Sep-18.
CSE 154 Lecture 11: More Events.
>> Dynamic CSS Selectors
Javascript and JQuery SRM DSC.
Lecture 11: Keyboard Events
JavaScript and Events CS Programming Languages for Web Applications
JavaScript Basics Topics Review Important Methods Writing Functions
Lecture 11: Keyboard Events
CSc 337 Lecture 5: Grid layout.
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and Events CS Programming Languages for Web Applications
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

Create / Delete DOM Elements Handle Browser Events DOM Manipulation Create / Delete DOM Elements Handle Browser Events Manipulation & Events SoftUni Team Technical Trainers Software University http://softuni.bg

Table of Contents Manipulating the DOM Event Handling Create Elements Delete Elements Element Attributes Event Handling Attach / Detach Events

Have a Question? sli.do #JSCORE

DOM Manipulation Modify the DOM Tree

Creating New DOM Elements HTML elements are created with document.createElement This is called a Factory Pattern Variables holding HTML elements are live: If you modify the contents of the variable, the DOM is updated If you insert it somewhere in the DOM, the original is moved Text added to textContent will be escaped Text added to innerHTML will be parsed and turned into actual HTML elements  beware of XSS attacks!

Creating New DOM Elements: Examples let list = document.createElement("ul"); let liPeter = document.createElement("li"); liPeter.textContent = "Peter"; list.appendChild(liPeter); let liMaria = document.createElement("li"); liMaria.innerHTML = "<b>Maria</b>"; list.appendChild(liMaria); document.body.appendChild(list);

Problem: List of Items Create a HTML page holding a list of items + text box + button for adding more items to the list Write a JS function to append the specified text to the list

Problem: List of Items – HTML <h1>List of Items</h1> <ul id="items"><li>First</li><li>Second</li></ul> <input type="text" id="newItemText" /> <input type="button" value="Add" onclick="addItem()"> <script> function addItem() { // TODO: add new item to the list } </script>

Solution: List of Items function addItem() { let text = document.getElementById('newItemText').value; let li = document.createElement("li"); li.appendChild(document.createTextNode(text)); document.getElementById("items").appendChild(li); document.getElementById('newItemText').value = ''; } Check your solution here: https://judge.softuni.bg/Contests/328

Deleting DOM Elements <ul id="items"> <li class="red">Red</li> <li class="blue">Blue</li> </ul> let redElements = document.querySelectorAll("#items li.red"); redElements.forEach(li => { li.parentNode.removeChild(li); });

Problem: Add / Delete Items Extend the previous problem Implement [Delete] action as link after each list item

Problem: Add / Delete Items – HTML <h1>List of Items</h1> <ul id="items"></ul> <input type="text" id="newText" /> <input type="button" value="Add" onclick="addItem()"> <script> function addItem() { ... function deleteItem() { ... } } </script>

Solution: Add / Delete Items function addItem() { let text = document.getElementById('newText').value; let li = document.createElement("li"); li.appendChild(document.createTextNode(text + " ")); let span = document.createElement('span'); span.innerHTML = "<a href='#'>[Delete]</a>"; span.firstChild.addEventListener('click', deleteItem); li.appendChild(span); document.getElementById("items").appendChild(li); document.getElementById('newText').value = '';

Solution: Add / Delete Items (2) function deleteItem() { let li = this.parentNode.parentNode; let ul = li.parentNode; ul.removeChild(li); } this.parentNode.parentNode == li this.parentNode == span this holds the clicked hyperlink Check your solution here: https://judge.softuni.bg/Contests/328

Problem: Delete from Table <table border="1" id="customers"> <tr><th>Name</th><th>Email</th></tr> <tr><td>Eve</td><td>eve@gmail.com</td></tr> <tr><td>Nick</td><td>nick@yahooo.com</td></tr> <tr><td>Didi</td><td>didi@didi.net</td></tr> <tr><td>Tedy</td><td>tedy@tedy.com</td></tr> </table> Email: <input type="text" name="email" /> <button onclick="deleteByEmail()">Delete</button> <div id="result" />

Solution: Delete from Table function deleteByEmail() { let email = document.getElementsByName("email")[0].value; let secondColumn = document.querySelectorAll( "#customers tr td:nth-child(2)"); for (let td of secondColumn) if (td.textContent == email) { let row = td.parentNode; row.parentNode.removeChild(row); document.getElementById('result'). textContent = "Deleted."; return; } document.getElementById('result').textContent = "Not found."; Check your solution here: https://judge.softuni.bg/Contests/328

Practice: DOM and Events Live Exercises in Class (Lab)

Browser Events and DOM Events Handling Events Browser Events and DOM Events

Handling Events in JS Browsers send events to notify the JS code of interesting things that have taken place <div id='text'>Some text</div> let div = document.getElementById('text'); div.onmouseover = function(event) { event.target.style.border = "3px solid green"; } div.onmouseout = function() { this.style.border = ""; // this === event.target

Event Types in DOM API Mouse events Touch events DOM / UI events click mouseover mouseout mousedown mouseup touchstart touchend touchmove touchcancel load (finished loading) unload (exit from page) resize (window resized) dragstart / drop Keyboard events Focus events Form events keydown keypress (emit char) keyup focus (got focus) blur (lost focus) input (value changed) change (change + leave) submit (form sent) reset (form reset) Learn more at https://developer.mozilla.org/docs/Web/Events

Add / Remove Event Handler let textbox = document.createElement('input'); textbox.type = 'text'; textbox.value = "I am a text box"; document.body.appendChild(textbox); textbox.addEventListener('focus', focusHandler); function focusHandler(event) { textbox.value = "Event handler removed"; textbox.removeEventListener('focus', focusHandler); } Subscribe to events like this, don't use onclick / onfocus

Problem: Stopwatch A HTML page holds time-box + [Start] + [Stop] buttons Implement the missing JS function stopwatch() Increase the time at each second Disable / enable buttons <div id="time" style="border:3px solid blue; text-align:center; font-size:2em; margin-bottom:10px">00:00</div> <button id="startBtn">Start</button> <button id="stopBtn" disabled="true">Stop</button> <script>window.onload = function() { stopwatch(); }</script>

Solution: Stopwatch function stopwatch() { let time, intervalID; let startBtn = document.getElementById('startBtn'); let stopBtn = document.getElementById('stopBtn'); startBtn.addEventListener('click', function() { time = -1; incrementTime(); intervalID = setInterval( incrementTime, 1000); startBtn.disabled = true; stopBtn.disabled = false; });

Solution: Stopwatch (2) stopBtn.addEventListener('click', function() { clearInterval(intervalID); startBtn.disabled = false; stopBtn.disabled = true; }); function incrementTime() { time++; document.getElementById('time').textContent = ("0" + Math.trunc(time / 60)).slice(-2) + ':' + ("0" + (time % 60)).slice(-2); } Check your solution here: https://judge.softuni.bg/Contests/328

Problem: Mouse in Gradient A HTML page holds linear gradient box Moving the mouse should show percentage [0% … 100%], depending on the location of mouse Left side  0%; middle  50%; right side  100%

Problem: Mouse in Gradient – HTML <head> <title>Mouse in Gradient</title> <link rel="stylesheet" href="gradient.css" /> <script src="gradient.js"></script> </head> <body onload="attachGradientEvents()"> <div id="gradient-box"> <div id="gradient">Click me!</div> </div> <div id="result"></div> </body> </html>

Problem: Mouse in Gradient – CSS #gradient-box { width: 300px; border: 2px solid lightgrey; } #gradient-box:hover { border: 2px solid black; #gradient { height: 30px; color: white; text-shadow: 1px 1px 10px black; text-align: center; line-height: 30px; background: linear-gradient( to right, black, white); cursor: crosshair; }

Solution: Mouse in Gradient function attachGradientEvents() { let gradient = document.getElementById('gradient'); gradient.addEventListener('mousemove', gradientMove); gradient.addEventListener('mouseout', gradientOut); function gradientMove(event) { let power = event.offsetX / (event.target.clientWidth - 1); power = Math.trunc(power * 100); document.getElementById('result').textContent = power + "%"; } function gradientOut(event) { document.getElementById('result').textContent = ""; }; Check your solution here: https://judge.softuni.bg/Contests/328

Problem: Highlight Active A webpage contains multiple input boxes inside divs Apply styling to the div that holds the focused input box Set class "focus" for active Remove class attribute from inactive

Problem: Highlight Active (2) <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"><title>Focus</title> <link rel="stylesheet" href="focus.css" /> <script src="focus.js"></script> </head> <body onload="focus()"> <div> <div><h1>Section 1</h1><input type="text"/></div> <div><h1>Section 2</h1><input type="text"/></div> <div><h1>Section 3</h1><input type="text"/></div> <div><h1>Section 4</h1><input type="text"/></div> </div> </body> </html>

Problem: Highlight Active (3) Place all project files in the same folder Listen for focus and blur events div { width: 470px; } div div { text-align: center; display: inline-block; width: 200px; height: 200px; margin: 15px; border: 1px solid #999; .focused { background: #999999; focus.css function focus() { // TODO } focus.js

Solution: Highlight Active function focus() { let inputs = document.getElementsByTagName('input'); Array.from(inputs).forEach(i => { i.addEventListener('focus', (event) => { event.target.parentNode.className = 'focused'; }); i.addEventListener('blur', (event) => { event.target.parentNode.removeAttribute('class'); }

Problem: Dynamic Validation A webpage contains a single email input field Display real-time feedback for user's input Valid input format: <name>@<domain>.<extension> Only lowercase Latin letters are allowed for all parts Apply class "error" when input is invalid .error { border: 2px solid red; } <label for="email">Enter email:</label> <input id="email" type="text"/>

Solution: Dynamic Validation function validate() { document.querySelector('input') .addEventListener('change', onChange); let regex = /^([\w\-.]+)@([a-z]+)(\.[a-z]+)+$/; function onChange(event) { if (!regex.test(event.target.value)) event.target.className = 'error'; else event.target.removeAttribute('class'); }

Practice: DOM and Events Live Exercises in Class (Lab)

Summary Modifying DOM elements: Handling events: let menu = document.getElementById('menu'); menu.style.display = 'none'; menu.appendChild( document.createElement('hr')); let link = menu.children[0]; menu.removeChild(link); let menu = document.getElementById('menu'); menu.onclick = function(event) { … }

DOM Manipulation https://softuni.bg/courses/ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.