Validate the error value of the confirmation message box

I wrote a script to create a confirmation popup window. However, when I implement it in our aspx.cs page, it seems to be returning incorrect values. Here is my script:

<script type="text/javascript">
function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    if (confirm("Do you want to remove this employee from this group?")) {
        confirm_value.value = "1";
    } else {
        confirm_value.value = "2";
    }
    document.forms[0].appendChild(confirm_value);
}

Button

<asp:Button ID="btnAdd"  OnClientClick = "Confirm()" runat="server"    ValidationGroup="1" onclick="btnAdd_Click" />

Button click function

 protected void btnAdd_Click(object sender, EventArgs e)
    {
   string confirmValue = Request.Form["confirm_value"];
        if (confirmValue == "1")
        {
          //Do something
        }
      else
       {
    //Do something
       }

    }

When I click cancel the first time, confirmvalue=="1", but then if I click ok afterwards, instead of getting 2, it shows confirmvalue=="1,2". Why is it giving an error value like this?

Answer №1

To prevent creating multiple inputs named "confirm_value" every time the button is clicked, you should reuse the existing input:

function Confirm() {
    var confirm_value = document.querySelector('[name="confirm_value"]');
    if (!confirm_value) {
        confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        document.forms[0].appendChild(confirm_value);
    }
    if (confirm("Do you want to remove this employee from this group?")) {
        confirm_value.value = "1";
    } else {
        confirm_value.value = "2";
    }    
}

Answer №2

function ConfirmEmployeeRemoval() {
var userChoice = confirm("Are you sure you want to delete this employee from the group?");
var confirmationValue = document.querySelector('[name="confirmation_value"]');
 if (userChoice) {
return true;
 } else {
return false;
}    
}

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

How to remove the initial negative value present in an array using C#

I have searched for various solutions, but none of them effectively address my requirement to eliminate the initial negative number in a sequence while retaining the others (there may be several negative numbers in the array). ...

Behind the scenes, unable to launch due to Schema Error

My experience with Backstage was going smoothly until I ran into an issue after executing a yarn install this afternoon. Now, whenever I attempt to run yarn dev, it fails with the following error: [0] Loaded config from app-config.yaml [0] <i> [webpa ...

What is the method for selecting the element currently under the mouse cursor?

Is it possible to change the color of all elements you hover over using plain Javascript without jQuery? HTML <ul> <li></li> <li></li> <li></li> </ul> JavaScript (function() { var item = ...

"Step-by-step guide for incorporating a mask in the Ant Design Datepicker component in React

Could someone help me figure out how to add a mask in the antd datepicker and validate it to ensure the correct date format (dd/mm/yy)? Here is the code I have attempted: <FormItem label="Date of Birth"> {getFieldDecorator("dob", {rules: [{ require ...

AngularJS ngAnimate triggering prematurely

In the current setup, p2 animates in while p1 is still animating out. After that, p1 disappears and p2 glitches up the page. The desired effect is for 1 to fade out and then have 2 fade in. html: <nav> <a ng-click="changeView('p1' ...

In C#, the process of defining a variable

I am in the process of creating a console application using Visual Studio that involves working with SQL. The database table I am dealing with has a significant number of columns, and I need to assign them as variables in a way that is effective for effici ...

Preventing the removal of a choice by disabling it in the selector

I have a unique selector that is designed like this: <select id="patientSelector"> <option disabled selected style='display: none;' id="select0"> New Patients to Come</option> <option id="select1"></opt ...

Loopback: Unable to access the 'find' property as it is undefined

I've come across a few similar questions, but none of the solutions seem to work for me. So, I decided to reach out for help. I'm facing an issue while trying to retrieve data from my database in order to select specific parts of it within my app ...

Display information from a Google Sheet onto a leaflet map based on specified categories

I am currently facing some challenges while creating a map with markers using data from Google Sheet and leaflet. Despite my efforts, I have encountered a few bugs that are proving to be difficult to resolve: Group Filtering - Although I can successfully ...

Is it possible to have multiple forms on a master page?

Regarding the limitation of having only 1 runat="server" and <form> per master page, I am facing a dilemma with placing my login form on my website. Should I include it all on the master page or distribute some elements to the .aspx form? Currently, ...

Vue alert: A duplicate key with the value of '10' has been identified. This could potentially lead to an issue with updates

I've been encountering a persistent error that I can't seem to resolve: [Vue warn]: Duplicate keys detected: '10'. This issue is causing an update error in my application. Despite trying the following steps, the error continues to appe ...

Step-by-step guide on Setting up the bronto.sca.cart object using Bronto JSON

I have implemented the Bronto Tag Manager to track cart details successfully. After including the Bronto Commerce JavaScript snippet on my page, I am now able to create the bronto.sca object. While bronto.sca.config() and bronto.sca.id are returning valu ...

Issue with displaying record attribute as a text variable

I'm currently attempting to retrieve the attribute odata.type from a record so that I can adjust my EditForm based on its value. Strangely, when I utilize a constant to achieve this and print it with console.log, it appears as follows: ƒ HWType(_ref ...

Modify one individual css style / tag

Currently, I have a large React application that is utilizing Material UI 4.3.0. I attempted to remove the margin-top style of label + .MuiInput-formControl within a select Mui component. To achieve this, I used the 'overrides' tag in my App.js ...

Unleash full rotation capabilities in OrbitControls.js

Currently, I am utilizing OrbitControls.js to rotate the camera around a fixed point. My intention is not to restrict any vertical rotation - my goal is for the camera to smoothly circle the model (orbits around the pivot point). I experimented with remov ...

Preventing Browser Back Button Functionality in Angular 2

I'm currently working on an Angular 2 website and wondering if there is a way to either disable or trigger the browser's back button using Angular 2. Any insights would be greatly appreciated! ...

Steps for adding an array of JSON objects into a single JSON object

I have a JSON array that looks like this: var finalResponse2 = [ {Transaction Amount: {type: "number"}}, {UTR number: {type: "string"}} ] My goal is to convert it into the following format: responses : [ { Transaction Amount: {type: "number"}, UTR numbe ...

Exploring the realm of unit testing in the NestJS CQRS architecture journey

We're currently working on writing unit tests using Jest for our application and are facing difficulties in testing sagas. Specifically, we're having trouble testing the saga itself. During our unit testing process, we've encountered an iss ...

What is the purpose of the hidden tag that accompanies a checkbox?

Similar Question: ASP.NET MVC: Why is Html.CheckBox generating an extra hidden input? While working on an asp.net mvc project, I noticed that when rendering a checkbox control, it also generates an additional hidden field like the examples below: < ...

Exploring JSON with JavaScript

[ {"lastName":"Noyce","gender":"Male","patientID":19389,"firstName":"Scott","age":"53Y,"}, {"lastName":"noyce724","gender":"Male","patientID":24607,"firstName":"rita","age":"0Y,"} ] The data above represents a JSON object. var searchBarInput = TextInput. ...