3

I created a simple lottery ticket and I made selector with toggle method. This is my code.

  $( "span" ).click(function() {
  $( this ).toggleClass( "span-selected" );
});

The toggle functionality works fine but I want to add a limitation so that only 7 numbers can be chosen in one container. Is there a way to achieve this. Here is my JSBIN > http://jsbin.com/menawu/1/edit?js,output

2
  • I can't go on Jsbin, I imagine when a number is selected when you click on a span ? Commented Nov 24, 2016 at 14:39
  • @Alexis Hi alex here is codepen codepen.io/Karadjordje/pen/Pbjmxy Commented Nov 24, 2016 at 14:42

6 Answers 6

5

You need to check if there are already 7 elements checked in that container, like so:

$( "span" ).click(function() {
  if (
     $(this).hasClass("span-selected") || 
     (!$(this).hasClass(".span-selected") && $(this).closest(".num-cont").find(".span-selected").length < 7)
  ) {
       $( this ).toggleClass( "span-selected" );
   }
});

So your criteria are:

  • if it's not selected, check if there are less than 7: if yes, toggle, otherwise don't do anything
  • if it is selected, unselect it.
Sign up to request clarification or add additional context in comments.

1 Comment

Very compact, but you are checking hasClass twice. This looks like it should be an exclusive or, so you could just check $(this).hasClass("span-selected") != $(this).closest(".num-cont").find(".span-selected").length < 7
2

You can use this code;

$( "span" ).click(function() {
  if($(this).parent().parent().find('.span-selected').length===7){
    alert('Limit');
  }
  else{
    $( this ).toggleClass( "span-selected" );
  }
});

Comments

2

Yes,

you can cumulate the count of tickets chosen in a variable and allow toggling only when count is less than 7, based on the jQuery hasClass method to check if your span was previously selected:

var countTicket = 0;
$( "span" ).click(function() {
  if($(this).hasClass( "span-selected")) {
    countTicket--;
    $( this ).toggleClass( "span-selected" );   
  } else if(countTicket<7) {
    $( this ).toggleClass( "span-selected" );
    countTicket++;
  }
});

1 Comment

This will count the tickets globally, while he needs to do it for each container. It is easier to use the length property.
2

Here an example, with multiple case for controle your numbers. You can easily know if it's unselect/select or if more than 7 span are selected by using hasClass/removeClass/addClass

$("span").click(function(){

  if($(this).hasClass("selected"))
  {
    $(this).removeClass("selected");
  }
  else{
    if($("span.selected").length<7)
    {
      $(this).addClass("selected");
    }
    else
      console.log("7 span selected");
  }
 
});
span{
  width:50px;
  height:50px;
  margin:10px;
  background-color:#eee;
  display:inline-table;
}

.selected{
  background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span>

1 Comment

Thank you for this solution.
1

just insert count and max variables

var max = 7;
var count = 0;
$("span").click(function() {
    if (count < max) {
        $(this).toggleClass("span-selected");
        count++;
    }
});

1 Comment

Good answer easy to understand but only works for one container.
0

You can get the number of selected item using the parent container and count them:

$( "span" ).click(function() {
          if($(this).closest('.num-cont').find('.span-selected').length==7){
            alert('Limit');
          }
          else{
            $( this ).toggleClass( "span-selected" );
          }
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.