farida
farida

Reputation: 1

if condition does not work in javascript

function confirmappt(target) {
    var type = document.getElementById("ultrasoundid").value
    var ultrasound = document.getElementById("hdultrasoundid").value
    var appt = document.getElementById('apptdocfacid').value
    appt = 'apptid' + appt + '_1'
    var checkedValue = 'test'
    for (var i = 0; i < 100; i++) {
        if (document.getElementsByName(appt)[i].checked) {
            checkedValue = 'ok'
            break;
        }
    }
    if (checkedValue == 'ok') {
        alert('Please select ultrasound type!');
    }
    if (confirm('Are you sure you want to make this appointment?')) 
      return true;
    return false;
}

if checkedValue is equal ok both alert and second if condition work. but checkedValue is not equal ok then second if condition does not work. Please help me to solve this issue.

Upvotes: -1

Views: 60

Answers (2)

Vivek Gupta
Vivek Gupta

Reputation: 1055

I understand from your problem statement that you want alert if checkValue is "ok" and confirm dialog when if checkValue is not "ok"only

use

if (checkedValue == 'ok') {
        alert('Please select ultrasound type!');

    }else{
       if (confirm('Are you sure you want to make this appointment?')) 
      return true;

}

Upvotes: 0

Satpal
Satpal

Reputation: 133403

You need to add else block

I think you need

if (checkedValue == 'ok') {
    alert('Please select ultrasound type!');
}else{  
    return confirm('Are you sure you want to make this appointment?');
}

Upvotes: 1

Related Questions