-1

I'm facing a problem with a jquery script. Can't enter on an if statement.

Can someone explain me why I cant enter on if (partner_type_id === 1) even if the value is 1?

$(document).ready(function(){

    $('#holder_0_partner_nr').change(function(){

        var partner_type_id = $("#holder_0_partner_type_id").val();
        var partner_nr = $("#holder_0_partner_nr").val();

        console.log(partner_type_id);
        console.log(partner_nr);

        if (partner_type_id === 1)
        {
            $.ajax({
                url: '{{ route('sgc.contracts.create.get.occ.person.data') }}',
                type: 'get',
                data: { partner_nr: partner_nr },
                success: function(response) {

                    if (response !== false)
                    {
                        let data = JSON.parse(response);

                        $("#holder_0_name").val(function() {
                            return data.name;
                        });

                    }

                }
            });
        }
    });

});
Jim G.
  • 15,141
  • 22
  • 103
  • 166
hhelderneves
  • 925
  • 8
  • 24

2 Answers2

1

are you sure it is a number? try parseInt(partner_type_id) === 1 OR partner_type_id === '1'?

Jeff I
  • 396
  • 1
  • 2
  • 13
1

.val() can return a number, string or array

In this case:

if (partner_type_id === 1) with triple ===(strict check), will only be true if both the value and type is equal.

Since you're comparing to a number you must make sure partner_type_id is also a number and not a string containing "1".

Either parse your variable correctly or use double ==

Colin Cheung
  • 148
  • 2
  • 13