0

The challenge is that I have checkboxes on the page. This code finds all the checkboxes on the page and if the checkbox is selected, it adds it to a separate block. I want to limit the amount that can be added to this block to 4x. And the idea is that if implement a counter in the widget, what will increase with each addition and decrease when the checkbox is removed, then I can control this process. Please help with this task.

<script>
    require([
        'jquery'
    ], function ($) {
        'use strict';
        $('input[type="checkbox"]').click(function(){
            if($(this).prop("checked") == true){
                $(".selected-items").append(($(this).closest(".sample-item-wrapper").clone()))
            }
            else if($(this).prop("checked") == false){
                var selected_item = $(this).closest(".sample-item-wrapper").attr('data-sample-id');
                $("div.selected-items div[data-sample-id='"+selected_item+"']").remove()
            }
        });
    });
</script>

Update


item:

<div data-sample-id="sample-2" class="sample-item-wrapper">
    <img src="<?php echo $this->getViewFileUrl('Dv_BuildingBedPage::images/image3.png'); ?>" />
    <div class="pretty p-default">
        <input type="checkbox" />
        <div class="state">
            <label></label>
        </div>
    </div>
</div>

selected items block:

<div class="selected-items-wrapper">
    <h4>Selected Samples</h4>
    <div class="selected-items"></div>
</div>
4
  • sample-item-wrapper is parent tag of your checkbox? Commented Jan 13, 2020 at 12:40
  • @Ranganathan updated the question please check Commented Jan 13, 2020 at 12:46
  • So you wanted to limit only 4 checkbox allowed to append right? Commented Jan 13, 2020 at 12:47
  • @Ranganathan yes Commented Jan 13, 2020 at 12:47

2 Answers 2

1

I have limited the checkbox count to 4. It will not append 5th checkbox. But still you can check those checkbox

<script>
    require([
        'jquery'
    ], function ($) {
        'use strict';
        $('input[type="checkbox"]').click(function(){
            if($(this).prop("checked") == true){
                if($('.selected-items .sample-item-wrapper').length < 4){
                    $(".selected-items").append(($(this).closest(".sample-item-wrapper").clone()))
                }
            }
            else if($(this).prop("checked") == false){
                var selected_item = $(this).closest(".sample-item-wrapper").attr('data-sample-id');
                $("div.selected-items div[data-sample-id='"+selected_item+"']").remove()
            }
        });
    });
</script>
10
  • but could you help translate this into a widget? if you do not mind Commented Jan 13, 2020 at 12:55
  • Is that working? Commented Jan 13, 2020 at 12:56
  • ok let me try and get back to you on tomorrow..Now I have to leave from office Commented Jan 13, 2020 at 12:57
  • Your option works but stops at 5 Commented Jan 13, 2020 at 13:00
  • Remove =. I have updated Commented Jan 13, 2020 at 13:02
1

This is more question from js.

You can try this:

 <script>
require([
  'jquery'
], function($) {
  'use strict';

  function updateCounter() {
    var len = $(".sample-item-wrapper input[name='YourCheckboesxName[]']:checked").length;
    if (len > 0) {
      $("#YourDivWithNumber i .counter").text('(' + len + ')');
    } else {
      $("#YourDivWithNumber i .counter").text(' ');
    }
  }
});
</script>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.