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 3229220
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:47:08+00:00 2026-05-17T16:47:08+00:00

This much I have already cobbled together (thanks to Stack Overflowers): I have a

  • 0

This much I have already cobbled together (thanks to Stack Overflowers):

I have a series of product modules, each of which contains a product name and a checkbox. Checking a checkbox adds the corresponding product name to a list elsewhere on the page, un-checking a checkbox removes the coresponding product name from the list. When the user submits the ‘Compare’ button, I need jQuery to check the number of selections, confirming a minimum of two and an arbitrary maximum.

My problem: the following jQuery is apparently failing to account for items which have been removed (by unchecking). For example, if I check a checkbox, then uncheck it – and it does get unchecked, and it does get removed from the list – and then re-check it, jQuery reports that there are 2 items to be compared.

<div class="product-module">
<div class="product-pic">
    <div class="checkbox-wrapper">
        <label for="compare1">
            <input type="checkbox" class="checkbox" name="compare" id="compare1" />
            Compare
        </label>
    </div>
</div>

<div class="product-info">
    <p><a href="#" title="#"><span class="product-name">Product Name here</span></a></p>    
</div>
</div>


<div class="compare">
<ul>
</ul>
<p class="compare-button"><button type="submit">Compare</button></p>
<p class="clear-selections"><a class="button" id="clear-selections" href="#">Clear Selections</a></p>
</div>

Script:

// clear all checkboxes on load
$(function(){
    $('input[type="checkbox"]').attr('checked', false);
});


    $(function(){
    $('.column-main input[type="checkbox"]').click(function()    {
        var title = $(this).closest('.product-module').find('.product-name').html();

        // if user checks the checkbox, add the item to the ul
        if    ($(this).attr('checked'))    {
        var html = '<li><a href="'+title+'">' + title + '</a></li>';
        $('.compare ul').append(html);

        // if user un-checks the checkbox, remove the item from the ul
        }    else if ($(this).attr('checked', false))    {
        $('li a[href="' + title + '"]').remove();
        }
    })
})


$(function(){
    $('.clear-selections').click(function(){
        $('.compare ul').empty();
        $('input[type="checkbox"]').attr('checked', false);
    })
});

    $(function(){
    $('.compare button').click(function(){
        minRequests = 2;
        maxRequests = 3;
        requested = $('.compare ul li').size();    // go figure: why not .length()?

        if(requested < 2)    {
        alert ('Compare ' + requested + ' products?');

        } else if((requested >= 2) && (requested <= 5 ))    {
        alert ('There are ' + requested + ' products to compare');

        } else {
        alert (requested + ' is too many');
        }
    });
});

  • 1 1 Answer
  • 1 View
  • 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-05-17T16:47:08+00:00Added an answer on May 17, 2026 at 4:47 pm

    The problem is here:

    $('li a[href$="' + title + '"]').remove();
    

    You’re removing the ‘a’ tag from inside the ‘li’, but leaving the ‘li’ – which is what you count later.

    Try:

    $('li a[href$="' + title + '"]').parent().remove();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This post on SO answers most of the questions I have (much thanks to
Hey! This is what I have already done so far which works great: Download
Note: This is question to which I have already found an answer. It seems
Yes, I have already asked this question, but the problem is much more specific.
I have done this without much effort on T-SQL, but I want to implement
I really have no idea why I'm struggling with this so much. I'm scraping
This might be a simple one, but since I don't have much knowledge about
This is pretty much the same problem i have, except with very different code:
I have an ajax loader much like this one: $('#loadingDiv') .hide() // hide it
Without showing too much code: I have an Activity A. This Activity populates a

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.