Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:56:26+00:00 2026-06-10T00:56:26+00:00

I am trying to add and remove <li> ‘s to a <ul> using the

  • 0

I am trying to add and remove <li>‘s to a <ul> using the data from within the class add-to-favourites, the problem is that when I have more than one word or a space between words the jQuery no longer recognises that it is the same word and adds the same value to the <ul> again.

You can see the problem here: http://jsfiddle.net/ZWxBb/

Try clicking on the rows of text you will see they are added and removed to the <ul> except for the first instance of class which is just added everytime it is clicked.

It would also be great if you could use more than 2 words as the element text, plus add a div which when clicked removed the item from favourites

HTML:

<div class="add-to-favourites">Two Words</div>
<div class="add-to-favourites">OneWords</div>
<div class="add-to-favourites">NoSpaces</div>
<ul id="favourites"></ul>

​
jQuery:

$(function () {
$('.add-to-favourites').click(function() {
   var num = $(this).text();
   var $fav = $('#favourites');
   var $fav_li = $fav.find('.' + num + '_li');

   if ($fav_li.length) {
      $fav_li.remove();
   } else {
      $('#favourites').append('<li class="' + num + '_li">' + num + '</li>');
   }
})
});

​
Thanks,
Ben

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:56:27+00:00Added an answer on June 10, 2026 at 12:56 am

    As lzzey said, class names cannot contain spaces. Try doing a .replace(” “, “_” ) on your class matching:

    $('.add-to-favourites').click(function() {
       var num = $(this).text();
       var numClass = num.replace(" ", "_" );
       var $fav = $('#favourites');
       var $fav_li = $fav.find('.' + numClass  + '_li');
    
       if ($fav_li.length) {
          $fav_li.remove();
       } else {
          $('#favourites').append('<li class="' + numClass  + '_li">' + num + '</li>');
       }
    })
    

    http://jsfiddle.net/ZWxBb/2/

    UPDATE:

    I would suggest using something other that the text to identify the li’s

    Html:

    <div class="add-to-favourites" data-item="1">Two Words</div>
    <div class="add-to-favourites" data-item="2">One asd asd asd asd Words</div>
    <div class="add-to-favourites" data-item="3">NoSpaces</div>
    

    Then js:

    $('.add-to-favourites').click(function() {
       var num = $(this).data( "item" );
       var text = $(this).text();
       var $fav = $('#favourites');
       var $fav_li = $fav.find('.' + num + '_li');
    
       if ($fav_li.length) {
          $fav_li.remove();
       } else {
          $('#favourites').append('<li class="' + num + '_li">' + text + '</li>');
       }
    })
    

    http://jsfiddle.net/ZWxBb/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to remove a class attribute from an element and add the class
I have a Swing browser application with a bug that as I add/remove to/from
I have a bit of a problem. I'm trying to add,remove and edit items
I'm trying to add and remove some rows from a table using Jquery. But
I'm having a couple problems trying to add/remove a class from links and their
I am trying to add and remove a class within a <span> , but
I am trying to add and remove events from a timer and I have
I'm trying to create a player who can add and remove items from their
I'm trying to use JQuery to add/remove classes as part of a function that
Is there any preexisting class that helps support add/remove EventListener operations? (kind of like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.