Search code examples
javascriptconfirm

confirmation dialog isn't showing


document.querySelector('#select').addEventListener("change", function() {
    var confirm = confirm("Do you want to update data?");
    if (confirm == true) {
          if (this.value == "1") {
            $.ajax({
                        url: "update.php",
                        type: "POST",
                        data: {
                            id: <?php echo $row['serial']?>,
                            type: "pending"         
                        },
                        cache: false,
                        success: function(dataResult){
                            var dataResult = JSON.parse(dataResult);
                            if(dataResult.statusCode==200){
                                alert("Successfully updated");

                                location.replace("../deposit/");
                            }
                            else if(dataResult.statusCode==201){
                                alert("Something went wrong");

                            }else{
                                alert("Everything went wrong");
                            }
                            
                        }
            });
          }else if(this.value == "2"){
            $.ajax({
                        url: "update.php",
                        type: "POST",
                        data: {
                            id: <?php echo $row['serial']?>,
                            type: "succeed"         
                        },
                        cache: false,
                        success: function(dataResult){
                            var dataResult = JSON.parse(dataResult);
                            if(dataResult.statusCode==200){
                                alert("Successfully updated");

                                location.replace("../deposit/");
                            }
                            else if(dataResult.statusCode==201){
                                alert("Something went wrong");

                            }else{
                                alert("Everything went wrong");
                            }
                            
                        }
            });
          }else if(this.value=="3"){
            $.ajax({
                        url: "update.php",
                        type: "POST",
                        data: {
                            id: <?php echo $row['serial']?>,
                            type: "canceled"            
                        },
                        cache: false,
                        success: function(dataResult){
                            var dataResult = JSON.parse(dataResult);
                            if(dataResult.statusCode==200){
                                alert("Successfully updated");

                                location.replace("../deposit/");
                            }
                            else if(dataResult.statusCode==201){
                                alert("Something went wrong");

                            }else{
                                alert("Everything went wrong");
                            }
                            
                        }
            });
          }else{
            console.log("Update canceled");
          }


}
});

I added a confirmation dialog while user changes option. When I run the code, and changed option I am not getting any confirmation dialog but, when I was working without confirmation dialog everything was working fine. Why it's happening? I got error in console Uncaught TypeError: confirm is not a function at HTMLSelectElement.<anonymous> I was looking at w3schools courses but, i am unable to understand anything


Solution

  • You should avoid using the name of HTML and Window objects and properties:

    https://www.w3schools.com/js/js_reserved.asp

    don't use confirm use ConfirmDialog or something!