Home > Net >  jquery if multiple values from foreach database
jquery if multiple values from foreach database

Time:04-06

I want to make a condition if a value is more than another value then the element will be disabled, but I am confused because the variable I want to compare has multiple values from foreach database, I have tried to make a condition using jquery but it takes the highest value of a variable.

 $(".fill-div").click(function () {
        var ongkir = $('#ongkir').val();
        var ongkirNum = parseInt( ongkir.replace('.',''));

        $('.diskon-data').each(function() {
            var minDiscount = $(this).data('minongkir');

            if (ongkirNum < minDiscount) {
                $(".diskon-data").addClass("disabled");
            }
            });

    });

The result I want is to disable certain elements that do not meet the conditions according to the conditions and enable other elements that do.

Please, help me :)

CodePudding user response:

You need to keep operating on this within your .each callback in order to make changes only to that particular element

$(".fill-div").on("click", function () {
  const ongkirNum = parseFloat(ongkir.replace(".", ""));

  $(".diskon-data").each(function() {
    $(this).toggleClass("disabled", ongkirNum < this.dataset.minongkir);
  });
});

Using $(".diskon-data") selects all the elements with that class.

  • Related