Home > Net >  Checking if the value of checkbox exist in an array of integer and checking it accordingly (Jquery)
Checking if the value of checkbox exist in an array of integer and checking it accordingly (Jquery)

Time:10-13

I have an array of intergers and I want to loop over all the checkboxes on my page and if the value of any checkbox exists in that array make it checked. It's an asp.net MVC project and its happening inside an ajax call below is the code. everything is working fine just the if statement isn't working data is getting properly I even console log everything one by one.

            function loadpackagefeatures() {
         var PackageId = "@Model.PackageId";
        let typeid = $("#typeId").find(":selected").val()
        $.ajax(
            {
                type: "POST", //HTTP POST Method
                url: '@Url.Action("GetCheckedFeatures","Home")', // Controller/View
                data: { //Passing data
                    Id: PackageId,
                    PkgType: typeid,
                },
                traditional: true,
                success: function (data, status, xhr) {
                    var CheckedPackageFeaturesIds = data.PackageFeaturesIds
                    console.log(CheckedPackageFeaturesIds);

                    $('input[type=checkbox]').each(function () {
                        var currentValue = this.value
                       //if (CheckedPackageFeaturesIds.includes(this.value)) //this is also not working
                        if (array.indexOf(currentValue) !== -1) {
                            ($(this).attr('checked', 'checked'))
                        }
                        else {
                            console.log("not found");
                        }
                    })
                },

            }
        );
    }

CodePudding user response:

Note that this.value is always a string. As your array is an integer array ("I have an array of [integers]"), indexOf will not match.

You need to either convert the array to string or this.value to integer, eg:

var arr = [2, 3];  // array of integers

$('input[type=checkbox]').each(function() {
  var currentValue = this.value * 1;  // or  this.value or other conversion
  if (arr.indexOf(currentValue) !== -1) {
    $(this).attr('checked', 'checked');
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type='checkbox' value='1'>
<input type='checkbox' value='2'>
<input type='checkbox' value='3'>
<input type='checkbox' value='4'>

When comparing int/string, they do not match:

var arr = [2, 3];  // array of integers

$('input[type=checkbox]').each(function() {
  var currentValue = this.value; // is a string
  if (arr.indexOf(currentValue) !== -1) {
    $(this).attr('checked', 'checked');
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type='checkbox' value='1'>
<input type='checkbox' value='2'>
<input type='checkbox' value='3'>
<input type='checkbox' value='4'>

  • Related