2

I've made a function which selects an item when you click on it. And made it so when I've selected more than 10, it stops adding to selectedItems.

But when 10 items is selected, I can still toggle the class d-items-selected by clicking. How do I disable that? I've tried to use stop() but that canceled the hole thing, so I couldn't 'de-select' the items again.

$(document).ready(function(){
        $('.d-items').on('click', function(e){
            e.preventDefault();
            $(this).toggleClass('d-items-selected');
            var selectedItems = $('.d-items-selected').length;
            if(selectedItems > 10) {
                $('.d-items').finish();
            } else {
                $('#ItemsSelected').html(selectedItems);
            }
        });
    });
6
  • Why don't you move toggle fn inside if statement? Commented Jul 7, 2016 at 18:27
  • off-topic to this question, but I was responding to your now-deleted question with: @zerovacpls as I stated earlier about the session array most likely to have not been destroyed (session_destroy();) and is keeping it in cache/same session. If you destroy the session array and try again, I'm betting it will work. I've seen that happen often before. You should also add an exit; after your header as your code may want to continue to execute further down. You also need to check if the session was started inside all other files using those (sessions). This might even be a caching issue. Commented Sep 18, 2016 at 14:06
  • ...you never said why you deleted your question and if you did find the problem; was it what I mentioned about the session array still being in memory? this being for your deleted question stackoverflow.com/q/39558222 - this is a "need to know" question. Commented Sep 18, 2016 at 14:07
  • I'm gonna try that - thank you. I deleted my question, because it was badly written. And for that I'm sorry. Commented Sep 18, 2016 at 14:11
  • @zerovacpls You're welcome. If what I mentioned for you to try worked, let me know (ping me directly). You could then reopen your question and I'll post an answer for it along with some more information to improve on it. Commented Sep 18, 2016 at 14:22

5 Answers 5

2

You can disable controls which are not selected. Something like this.

$(document).ready(function(){
        $('.d-items').on('click', function(e){
            e.preventDefault();
            $(this).toggleClass('d-items-selected');
            var selectedItems = $('.d-items-selected').length;
            if(selectedItems > 10) {
                //do not allow to select
                $(this).removeClass('d-items-selected');
            } else {
                $('#ItemsSelected').html(selectedItems);
            }
        });
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry but this can select more than 10 items + it only counts to 9 when i select 10 of them
Thanks in advanced!
0

Would unbinding the click event work for you?

e.g.

if(selectedItems > 10) {
  $('.d-items').unbind("click");
}

Otherwise you can rebind it to a different function after selectedItems > 10, or anything really.

edit: It would help if you clarified what exactly you want to happen on click after selectedItems > 10

1 Comment

Nothing. When you already have selected 10 items, you should not be able to select more. But still be able to deselect an item
0

Maybe try e.stopPropagation() or e.stopImmediatePropagation()

Comments

0

I tried to figured out a solution:

$(function () {
  $('.d-items').on('click', function(e) {
    e.preventDefault();
    var selectedItems = $('.d-items-selected').length;

    //if selected items are less then 10
    // or the current item is already selected you can deselect
    if (selectedItems<10 || (selectedItems>=10 && $(this).is('.d-items-selected'))) {
      $(this).toggleClass('d-items-selected');
    }
    if (selectedItems > 10) {
      $('.d-items').finish();
    } else {
      $('#ItemsSelected').html(selectedItems);
    }
  });
});

5 Comments

When I select an item now, it says '0' then when I deselect it, it says 1 item selected..
My items arent checkboxes. It looks like this: gyazo.com/dc50220519aca321a2aace14036e7d8b So I do not understand your suggestion.
Hmm, when you click on an item now, it doesnt add 1 to 'items selected(top of the page)' but then when you select a second item it goes to 1. So if I select 2 items it says 1. and if i select 2 items and then deselect 1 of them it goes to 3. That's not how its supposed to do. If you have steam you can login the site and see it yourself. It's a bit hard to explain what it does.
Sorry, I only wrote a last update. If it could help ok. Thanks so much 😊
80.167.90.224/csgodeadly/deposit.php thats the link to the site. You just sign in through steam and then go to the deposit site :) I hope you have counterstrike, else this will not help
0
$(document).ready(function(){
            var i=0;
            $('.d-items').on('click', function(e){
                e.preventDefault(); 

                if($(this).hasClass('d-items-selected')) {
                    $(this).removeClass('d-items-selected');
                    i--;
                    console.log("deleted"+i);
                }
                else {
                    if(i<10) {
                        $(this).addClass('d-items-selected');
                        i++;
                        console.log("added"+i);
                    }
                }
            })
});   

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

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.