Search code examples
jqueryfunctionclicktarget

JQuery toggleClass() - How to toggle ONLY the targeted text


I'm doing an assignment where I'm creating a shopping list. Whenever I click the check button, the shopping item should switch to a class that puts a line through the text. I got the code working, but it's toggling ALL of the shopping items. Can someone provide some guidance?

Note: I cannot change the HTML or CSS; this is strictly a JavaScript assigment. Also, I'd love a basic nudge in the right direction. I'm not looking for a straight up answer. Lastly, I'm just starting the project, so I'm looking for help with JUST this issue. Thanks in advance!!

Here's a link to the page...

https://repl.it/@DustinVenable/ShoppingListAssignment


Solution

  • The problem is that you toggle the class on all elements. $(".shopping-item").toggleClass("shopping-item__checked");

    You can use $(this).closest("li").find(".shopping-item").toggleClass("shopping-item__checked"); that will only toggle the element that exist inside the li of the button your clicked.

    demo

    $(".shopping-item-delete").click(function() {
      $(this).parent().parent().remove();
    })
    
    //Check and Uncheck Shopping Items
    $(".shopping-item-toggle").click(function() {
      $(this).closest("li").find(".shopping-item").toggleClass("shopping-item__checked");
    });
    * {
      box-sizing: border-box;
    }
    
    body {
      font-family: 'Roboto', sans-serif;
    }
    
    button,
    input[type="text"] {
      padding: 5px;
    }
    
    button:hover {
      cursor: pointer;
    }
    
    #shopping-list-item {
      width: 250px;
    }
    
    .container {
      max-width: 600px;
      margin: 0 auto;
    }
    
    .shopping-list {
      list-style: none;
      padding-left: 0;
    }
    
    .shopping-list>li {
      margin-bottom: 20px;
      border: 1px solid grey;
      padding: 20px;
    }
    
    .shopping-item {
      display: block;
      color: grey;
      font-style: italic;
      font-size: 20px;
      margin-bottom: 15px;
    }
    
    .shopping-item__checked {
      text-decoration: line-through;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="container">
      <h1>Shopping List</h1>
    
      <form id="js-shopping-list-form">
        <label for="shopping-list-entry">Add an item</label>
        <input type="text" name="shopping-list-entry" id="shopping-list-entry" placeholder="e.g., broccoli">
        <button type="submit">Add item</button>
      </form>
    
      <ul class="shopping-list">
        <li>
          <span class="shopping-item">apples</span>
          <div class="shopping-item-controls">
            <button class="shopping-item-toggle">
                <span class="button-label">check</span>
              </button>
            <button class="shopping-item-delete">
                <span class="button-label">delete</span>
              </button>
          </div>
        </li>
        <li>
          <span class="shopping-item">oranges</span>
          <div class="shopping-item-controls">
            <button class="shopping-item-toggle">
                <span class="button-label">check</span>
              </button>
            <button class="shopping-item-delete">
                <span class="button-label">delete</span>
              </button>
          </div>
        </li>
        <li>
          <span class="shopping-item shopping-item__checked">milk</span>
          <div class="shopping-item-controls">
            <button class="shopping-item-toggle">
                <span class="button-label">check</span>
              </button>
            <button class="shopping-item-delete">
                <span class="button-label">delete</span>
              </button>
          </div>
        </li>
        <li>
          <span class="shopping-item">bread</span>
          <div class="shopping-item-controls">
            <button class="shopping-item-toggle">
                <span class="button-label">check</span>
              </button>
            <button class="shopping-item-delete">
                <span class="button-label">delete</span>
              </button>
          </div>
        </li>
      </ul>
    </div>