Biomehanika
Biomehanika

Reputation: 1540

Conditional overwrites variable value

This jQuery structure stores selected value from an option list on selectedCourse variable, and a conditional checks if is different to 0. In that case it sends trought ajax selectedCourse value to a separate php file.

Problem: Before the conditional, variable stores selected numerical value. After it, it is overwriten with TRUE. In this case you can see ir clearly: first alert shows the numerical value selected, and second one shows TRUE.

Why is this conditional changing selectedCourse value? Shouldn't it just check if the condition is happening or not? Thank you.

$('#class_conf').on('change', '#cl_course', 'select', function () {
    var selectedCourse = $('#class_conf').find('#cl_course').val();
    alert(selectedCourse);
    if(selectedCourse = !0) {
        $.ajax({
            type: 'POST',
            url: 'config/forms/class_conf/class_form_subjects.php',
            data: {
                co_rel_co: selectedCourse
            }
        }).done(function (datos) {
            $('#cl_subject').prepend(datos);
        });
    }
    alert(selectedCourse);
});

Upvotes: 0

Views: 40

Answers (1)

Satpal
Satpal

Reputation: 133403

Condition should be selectedCourse != 0 instead of selectedCourse = !0

The value of !0 is true and you are assigning it to selectedCourse

Upvotes: 4

Related Questions