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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:02:16+00:00 2026-05-20T08:02:16+00:00

Im trying to figure out a simple way to enable me to select 2

  • 0

Im trying to figure out a simple way to enable me to select 2 DIV elements using JQuery – here is my attempt : http://jsfiddle.net/MarKP/5/

I need to limit the selections to just 2 and will use the class I add to get the selected objects.

Can anyone point me in a better direction

<div id="1">one</div>
<div id="2">two</div>
<div id="3">three</div>
<div id="4">four</div>

var selected = 0;
var prevSelect;
$('div').click(function() {
    if (selected == 2) {
        selected = 1;
        console.log(prevSelect);
        $('#' + prevSelect).removeClass('fill');
    }
    $(this).addClass('fill');
    prevSelect = $(this).attr('id');
    selected = selected +1;

});

div {
    border: 1px solid blue;
    height: 25px;
    margin-bottom: 5px;
}
.fill {
    background-color: red;
}
  • 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-05-20T08:02:17+00:00Added an answer on May 20, 2026 at 8:02 am

    I updated your functionality to disallow any selection change if 2 divs are already selected unless you click a selected div to unselect it:

    http://jsfiddle.net/MarKP/32/

    $('div').click(function(e){
        var $et = $(e.target);
        if ($et.hasClass('fill')) { 
            $et.removeClass('fill');
        } else {
            if ($('.fill').length < 2) {
                $et.addClass('fill');
            }
        }
    });
    

    Old solution: http://jsfiddle.net/MarKP/11/

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

Sidebar

Related Questions

So I'm trying to figure out the best way to solve simple logarithms using
Django (and database) newbie here. I'm trying to figure out a way to enable
this may be stupidly simple but I'm trying to figure out a way to
I am trying to figure out if there is a simple way to display
I'm trying to figure out the best way to handle a simple problem: I
I'm trying to figure out the easiest way to get the http://www.mydomain.com portion of
I am trying to figure out the most simple way to return 2 or
I'm using Core Data and I'm trying to figure out the simplest way to
I am trying to figure out a simple way of making a text box
I'm trying to figure out a simple way to handle reliability for UDP messages.

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.