Book Image

Advanced JavaScript

By : Zachary Shute
Book Image

Advanced JavaScript

By: Zachary Shute

Overview of this book

If you are looking for a programming language to develop flexible and efficient applications, JavaScript is an obvious choice. Advanced JavaScript is a hands-on guide that takes you through JavaScript and its many features, one step at a time. You'll begin by learning how to use the new JavaScript syntax in ES6, and then work through the many other features that modern JavaScript has to offer. As you progress through the chapters, you’ll use asynchronous programming with callbacks and promises, handle browser events, and perform Document Object Model (DOM) manipulation. You'll also explore various methods of testing JavaScript projects. In the concluding chapters, you'll discover functional programming and learn to use it to build your apps. With this book as your guide, you'll also be able to develop APIs using Node.js and Express, create front-ends using React/Redux, and build mobile apps using React/Expo. By the end of Advanced JavaScript, you will have explored the features and benefits of JavaScript to build small applications.
Table of Contents (9 chapters)

Chapter 3: DOM Manipulation and Event Handling


Activity 3 – Implementing jQuery

You want to make a web app that controls your home's smart LED light system. You have three LEDs that can be individually turned on or off, or all toggled together. You must build a simple HTML and jQuery interface that shows the on state of the lights. It must also have buttons to control the lights.

To utilize jQuery to build a functioning application, follow these steps:

  1. Create a directory for the activity and in that directory, in the command prompt, run npm run init to set up package.json.

  2. Run npm install jquery -s to install jQuery.

  3. Create an HTML file for the activity and give the HTML block a body.

  4. Add a style block.

  5. Add a div to hold all of the buttons and lights.

  6. Add a script tag with the source to the jQuery file.

    <script src="./node_modules/jquery/dist/jquery.js"></script>
  7. Add a script tag to hold the main JavaScript code.

  8. Add a light class to the CSS style sheet with the following settings:

    Width and height: 100px

    Background-color: white

  9. Add a toggle button to the div by using the id=toggle.

  10. Add a div to hold the lights with the id lights.

  11. Add three divs inside this div.

    Note

    Each div should have a div with the light class and a button with the lightButton class.

  12. In the code script, set up a function to run when the DOM loads:

    $( () => { ... } )
  13. Select all the lightButton class buttons and add on-click handler that does the following:

    Stops the event propagation and selects the element target and get the light div by traversing DOM.

    Check the lit attribute. If lit, unset the lit attribute. Otherwise, set it with jQuery.attr()

    Change the background-color css style to reflect the lit attribute with jQuery.css().

  14. Select the toggle button by ID and add an on click handler that does the following:

    Stops the event propagation and selects all the light buttons by CSS class and trigger a click event on them:


Code:

activity.html

The following is the condensed code. The full solution can be found at activities/activity3/index.html.

$( '.lightButton' ).on( 'click', e => {
  e.stopPropagation();
  const element = $( e.target ).prev();
  if ( element.attr( 'lit' ) !== 'true' ) {
    element.attr( 'lit', 'true' );
    element.css( 'background-color', 'black' );
  } else {
    element.attr( 'lit', 'false' );
    element.css( 'background-color', 'white' );
  }
} );
$( '#toggle' ).on( 'click', e => {
  e.stopPropagation();
  $( '.lightButton' ).trigger( 'click' );
} );

Snippet 3.32: jQuery function application

Outcome:

Figure 3.14: Adding buttons after each div

Figure 3.15: Adding toggle buttons

You have successfully utilized jQuery to build a functioning application.