Code Monkey home page Code Monkey logo

week_02-day_05-advanced-jquery's Introduction

Advanced jQuery

Event Delegation

Let's look at a click event that we have seen before.

<ul>
    <li>Riyadh</li>
    <li>Boston</li>
    <li>Jeddah</li>
</ul>
$("li").on("click", function(event){
    $(event.target).css("color", "red")
})

When we click on an li then the css font color fo that li changes to red.

However when we add a new li dynamically, the click event does not work on that li.

<ul>
    <li>Riyadh</li>
    <li>Boston</li>
    <li>Jeddah</li>
</ul>
$("li").on("click", function(event){
    $(event.target).css("color", "red")
})

// could also be added by a user submitting a form
$("ul").append("<li>New York</li>");

The click event works on the li for Riyadh, Boston, and Jeddah but not on New York. This is because the click event was added BEFORE the New York <li> was appended to the DOM.

We can fix this by using event delegation.

$("ul").on("click", "li", function(event){
    $(event.target).css("color", "red")
})

// could also be added by a user submitting a form
$("ul").append("<li>New York</li>");

Notice how we select a parent element that IS on the page when the page originally loads and we tell it to listen for a click even on its children that are an li.

Lab

  • Open the event-delegation/index.html file in the browser.
  • Add a new li by submitting the form
  • Notice how the click event works on the other lis but not the new one
  • Use event delegation in event-delegation/main.html to fix the problem so that the click event works on even dynamically added lis.

Group Project

With your group, you are going to build a fully functioning To Do List application using jQuery. The HTML and CSS has already been completed for you. It is up to you and your team to write the jQuery and Javascript to make the To Do List function. Every team member should be coding along together and have their own copy of the code. Work together and make sure every team member understands the code.

week_02-day_05-advanced-jquery's People

Contributors

micfin avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.