I am having trouble getting the JavaScript confirm function to function properly

Encountering a peculiar issue with the javascript confirm method - every time I call it, an error is thrown stating "Uncaught TypeError: Property 'confirm' of object [object Object] is not a function"

 var confirmVal = window.confirm("Please confirm?");
          if( confirmVal == true ){  

              return true;
          }

The structure of my js files in the html is as follows:

<link rel="stylesheet" href="css/vendor/bootstrap.min.css">    
<link rel="stylesheet" href="css/vendor/jquery-ui.css">


<script src="js/vendor/jquery-2.1.3.min.js"></script>
<script src="js/vendor/jquery-ui.js"></script> 


<link rel="stylesheet" href="css/vendor/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" href="css/vendor/star-rating.css">
<link rel="stylesheet" href="css/vendor/toastr.min.css">
<script type="text/javascript" src="cordova.js"></script> 
<script src="js/vendor/bootstrap.js"></script>
<script src="js/vendor/jquery.mobile-1.4.5.min.js"></script>
<script src="js/vendor/star-rating.min.js"></script>
<script type="text/javascript" src="js/vendor/toastr.min.js"></script>

Curiously enough, it works perfectly fine in another html file.. Any idea what could be going wrong here? Please assist.

Answer №1

Everything in the code is accurate except for the fact that the return statement needs to be inside a function.

function validation(){
  var confirmVal = window.confirm("Please confirm?");
      if( confirmVal == true ){  
          return true;
      }
  }

Answer №2


navigator.notification.confirm(
    'PLEASE CONFIRM YOUR CHOICE!', // message
     myFunction,            // callback to execute with the button index pressed
    'CONFIRMATION',           // title
    ['OK','CANCEL']     // button labels
);

function myFunction(index){
   console.log(index);
}

Answer №3

Using the return statement is important, but it's also crucial to ensure proper handling of that statement. To do this, your code should be wrapped in a function.

var confirmed = confirm("yes or cancel");

function confirmMe (){
     if (confirmed){
          return true;
     }
}

An alternative approach:

function confirmMe (){
     return confirmed;
}

// This will either return true or false

To avoid overriding the window object, you can check if the window has the confirm method by opening the console and typing 'window'.

Answer №4

Make sure to wrap the "return true" statement inside a function to execute properly. For more information on using the return statement in JavaScript, check out this link

var confirmVal = window.confirm("Please confirm?");
          if( confirmVal == true ){  
            alert("alright");             
          }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the Power of Merging HTML with JavaScript in Shopify

Here is a snippet of code that I am grappling with: <div id="next-shipment"></div> var date = new Date(); // today using client's timezone date.setDate(date.getDate() + 1); // move to tomorrow date.setUTCHours(11,0,0,0); // set time using ...

Angular filter - organizing objects based on a specific property or a combination of properties

I modified the Angular filter group by example by converting the team into an object. var app = angular.module('myApp',['angular.filter']); app.controller('MainController', ['$scope', function($scope){ $sc ...

How can I implement a feature to automatically close a drawer in a React application using the Material UI

Currently, I am utilizing a React material design theme However, I am facing an issue where the drawer navigation button on mobile view does not close automatically upon clicking I need something like (onClick={handleClose}) to resolve this problem Does ...

What is the best way to nest an <input> element within a <label> element in HTML?

Place an input field inside every label. Remember to place each input element after the label text, and make sure to include a space after the colon. <form> <label>Please Input Your First Name:</label> ...

Circular graphs displaying percentages at their center, illustrating the distribution of checked checkboxes across various categories

Looking for a JavaScript script that displays results in the form of circles with percentage values at their centers, based on the number of checkboxes checked in different categories. The circle radius should be determined by the percentage values - for e ...

The synchronization and network bandwidth optimization of Angular and Firebase's three-way binding system

Is it possible to synchronize the text box content across various clients using Angular and Firebase? I am curious to know if Firebase will update the database with each letter I type. How does this process impact network bandwidth? Can you explain how it ...

Tips for properly encoding HTML code before inserting it into a MySQL database

I have been using the tinymce editor to create an html page that I then insert into mysql. Here's what I've tried: $esdata = mysql_real_escape_string($data); This method works for all types of html formatting, except for images. For example, if ...

Stylish Material Design Icons

Our website utilizes CSS classes to display icons across various pages. .alert { /* used for "alert" type messages */ background: url(images/alerts.png) no-repeat left top; padding: 5px 0 15px 35px; margin: 0; } Use: <p id="ctl00_ctl00_ContentMessagePa ...

Is there a way to automatically reset an Angular scope variable when an HTML tag is executed?

Hi, I have a variable linked to my scope, $scope.isValid = { incorrectPhone: false } I am using a span to show a message when the variable becomes true ... <form name="myForm"> <span ng-if="isValid.incorrectPhone && myForm.$submitted" ...

What are the best strategies for mastering conditional promises in Angular?

I've been struggling with a problem in angularjs for several hours now. Here's the pseudo code I'm working with: doSomething(param){ var res; if(param = "ok"){ //perform some api calls with promises res = promise r ...

Switching the theme color from drab grey to vibrant blue

How can I change the default placeholder color in md-input-container from grey to Material Blue? I have followed the instructions in the documentation and created my own theme, but none of the code snippets seems to work. What am I doing wrong? mainApp. ...

Retrieving the date from the final item in a JSON object using AngularJS

In my web application built with AngularJS, I am dynamically creating objects. The goal is to allow users to load new events by clicking a button. To achieve this functionality, I need to determine the date of the last event loaded so that I can fetch the ...

How to restart a paused jQuery cycle by clicking on the pager

I currently have a jquery cycle in motion along with a pager. The cycle runs automatically, and there are pager buttons for manual control of the cycle; however, after using the pager buttons, it goes back to automatic cycling. I recently added an external ...

Steps to Create an HTML Text Box that cannot be clicked

Does anyone know of a way to prevent a text box from being clicked without disabling it or blocking mouse hover events? I can't disable the text box because that would interfere with my jQuery tool tips, and blocking mouse hover events is not an opti ...

Multiple instances of FontAwesome icons appearing in Bootstrap Navbar

Currently stuck on my navbar design, specifically regarding the duplication of icons: https://i.sstatic.net/f0pBR.png Working with Bootstrap 2.3.2 and FontAwesome 4.0.3. <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-comb ...

Leveraging JavaScript namespacing

I am exploring the concept of converting this code into a namespace to enhance clarity and prevent global namespace pollution. As someone relatively new to this, I would really appreciate some guidance or examples on how to achieve the conversion to a Java ...

sluggish jquery treeview loading

I recently implemented a jQuery treeview from a source I found at Initially, the treeview worked well when there were only a small number of nodes and subnodes. However, as the content grew, the loading time increased significantly to the point where usin ...

Trouble with the Bootstrap navbar loading correctly

Recently I started using Bootstrap and decided to implement one of their templates. Everything is working fine, except for my navbar which is not loading correctly. It should look like this: Desired Navbar However, in both Chrome and Firefox, it appears l ...

Error: Unable to access the 'push' property of an undefined value

42 | <Router> 43 | <ul> > 44 | {this.state.movies.map(movie => <li onClick={() => this.handleMovieClick(movie.title)}>{movie.title}{movie.description} <button type="button" onClick={() => this.deleteMovie(movie.title)}& ...

Is there a way to utilize a switch statement in JavaScript to determine the quantity of numerical elements within an array?

I am attempting to use a switch statement in my function to count the number of elements (numbers only) in an array. However, I am uncertain about how exactly the code should be structured. This is what I have managed to come up with thus far: <scrip ...