Detect when button is clicked in div with jquery

I would like to detect when a button is pressed in a div that has several buttons that act as checkboxes for a mobile optimized application. This is what I have as html:

enter image description here

When these were just regular flags instead of buttons, like flags, I was able to achieve what I want with this script:

$(document).on('change', 'input:checkbox.modifier', function () {
            var totalChecked = $("#modifiersDiv :checkbox:checked").size();
            var maximum = parseInt($('#Maximum').val());
            if (totalChecked === maximum) {
                $("#modifiersDiv :checkbox:not(:checked)").attr("disabled", true);
            } else {
                $("#modifiersDiv :checkbox:not(:checked)").attr("disabled", false);
            }
        });

Now I'm trying something like this just to see if the function will run at all, but without success:

 $(document).on('click', 'input:button.modifier', function () {
                   });

The bottom line is what I want to detect with jquery when 5 buttons are selected, then I will need to disable the other buttons in the div until the user deselects a button.

Any advice would be highly appreciated. Thanks in advance, Laziale

UPDATE: the code I use for the button:

var startDiv = "<div class='btn-group btn-block' data-toggle='buttons'>";
                            var checkBox = '';
                            $.each(data.modifierOptions, function(key, item) {
                                    checkBox += "<label class='btn btn-checkbox btn-block' style='margin-bottom:2px;'><input type='checkbox' data-quantity='1' class='modifier' data-itemid='" + itemid + "' data-name='" + item.Name + "' data-price='" + item.Price + "' name='" + item.Name + "' value='" + item.ID + "'/>" + item.Name + " </label><br />";
                            });
                            var endDiv = "</div>";
                            var totalDiv = startDiv + checkBox + endDiv;
                            $(totalDiv).appendTo('#modifiersDiv');
+4
3

, . , , : http://codepen.io/aecend/pen/mjklu

, , . jQuery checkbox , ( , ), "disabled" .

HTML, , btn-default, :

<html>
<head>
    <meta charset="utf-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <title>Hamburger Builder v2.0</title>
</head>
<body class="sitecolor">

<div id="modifiersDiv" class="modal-div" style="width: 250px; margin: 0 auto;">
    You can select a maximum of 5 option(s)
    <br>
    <div class="btn-group btn-block" data-toggle="buttons">
        <label class="btn btn-default btn-checkbox btn-block" style="margin-bottom: 2px;">
            <input class="modifier" type="checkbox" value="67739" name="Bacon" data-price="1" data-name="Bacon" data-itemid="a4007375-e037-46ce-be39-57ca2d1872e0" data-quantity="1">
            Bacon
        </label>
        <br>
        <label class="btn btn-default btn-checkbox btn-block" style="margin-bottom: 2px;">
            <input class="modifier" type="checkbox" value="67740" name="Lettuce" data-price="1" data-name="Lettuce" data-itemid="a4007375-e037-46ce-be39-57ca2d1872e1" data-quantity="1">
            Lettuce
        </label>
        <br>
        <label class="btn btn-default btn-checkbox btn-block" style="margin-bottom: 2px;">
            <input class="modifier" type="checkbox" value="67741" name="Tomato" data-price="1" data-name="Tomato" data-itemid="a4007375-e037-46ce-be39-57ca2d1872e2" data-quantity="1">
            Tomato
        </label>
        <br>
        <label class="btn btn-default btn-checkbox btn-block" style="margin-bottom: 2px;">
            <input class="modifier" type="checkbox" value="67742" name="Cheese" data-price="1" data-name="Cheese" data-itemid="a4007375-e037-46ce-be39-57ca2d1872e3" data-quantity="1">
            Cheese
        </label>
        <br>
        <label class="btn btn-default btn-checkbox btn-block" style="margin-bottom: 2px;">
            <input class="modifier" type="checkbox" value="67743" name="Onions" data-price="1" data-name="Onions" data-itemid="a4007375-e037-46ce-be39-57ca2d1872e4" data-quantity="1">
            Onions
        </label>
        <br>
        <label class="btn btn-default btn-checkbox btn-block" style="margin-bottom: 2px;">
            <input class="modifier" type="checkbox" value="67744" name="Pickles" data-price="1" data-name="Pickles" data-itemid="a4007375-e037-46ce-be39-57ca2d1872e5" data-quantity="1">
            Pickles
        </label>
        <br>
    </div>
</div>
</body>

JavaScript, , , , Bootstrap-ready:

$(document).on('change', 'input:checkbox.modifier', function () {
    var totalChecked = $("#modifiersDiv :checkbox:checked").size();
    var maximum = 5; //parseInt($('#Maximum').val())
    if (totalChecked === maximum) {
        $(this).parent().siblings(":not(.active)").addClass("disabled");
    } else {
        $("#modifiersDiv label.disabled").removeClass("disabled");
    }
});
+4

, :

$(".button:not(.selected)").click(function(){
   $(this).addClass("selected");
   if($(".button.select").length>4){
      $(".button").addClass("disabled");
   }
}

, -, , ...

0

Try the following :))))

var count=0;
$("#modifiersDiv").find(".modifier").click(function(){

   if($(this).is(":checked")){
    count++;
   }else{
    count--;
  }  

  if(count>=$('#Maximum').val())
   $("#modifiersDiv").find(".modifier").not(':checked').attr("disabled", true);
  else    
   $("#modifiersDiv").find(".modifier").not(':checked').attr("disabled", false);
  });
0
source

Source: https://habr.com/ru/post/1539730/


All Articles