Link to home
Start Free TrialLog in
Avatar of nish darsh
nish darsh

asked on

if..else condition in jquery for making ajax calls

Hello experts,

I am trying to call the ajax function only if there are values in 'stepname' and 'altmethod' else show and alert. When I include the following code, the else part gets executed all the time though there are no values. Could anyone kindly help me with this.

$(document).on("click", '#nextstep', function(e) {

    e.preventDefault();
 var stepname = $('#stepname').val(); 
 var chosen_altmethod = $('#chosen_altmethod').val();
 var altmethod = $('#altmethodname').val();

 
var formData = new FormData();
formData.append('stepname',stepname);
formData.append('altmethod',altmethod);
formData.append('chosen_altmethod',chosen_altmethod);
 formData.append('action','nextstep');

 console.log(stepname,"stepname");
console.log(altmethod, "altmethod");
console.log(chosen_altmethod,"chosen_altmethod");

if((stepname=="undefined") && (altmethodname=="undefined")){
  alert('please choose a step');
}else{
        $.ajax({
             type: 'POST',
            url: "<?= base_url() ?>index.php/variants/fetchstep1/",
             data: formData,
             processData: false,
      contentType: false,
             success: function (data) {

   $("#steplist").remove();
 $("#replacelist").html(data);
   $('html, body').animate({ scrollTop: 0 }, 0);
             }
         });
}
     });

Open in new window


Thank you in advance
ASKER CERTIFIED SOLUTION
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nish darsh
nish darsh

ASKER

Perfect.. Thank you very much for the quick reply.. :)
Avatar of Julian Hansen
To follow on from zephyr_hex's comments (not for points) there is a significant difference between the state of a variable being undefined and the string undefined.

When you put undefined in quotes you turn it into a string - the JS compiler has no way of knowing that you meant undefined - it has to interpret that as a string.
Real quick and helpful reply