Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Advanced JavaScript

You're reading from   Advanced JavaScript Speed up web development with the powerful features and benefits of JavaScript

Arrow left icon
Product type Paperback
Published in Jan 2019
Publisher
ISBN-13 9781789800104
Length 330 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Zachary Shute Zachary Shute
Author Profile Icon Zachary Shute
Zachary Shute
Arrow right icon
View More author details
Toc

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime