Display prompt before continuing

I have a button to delete checked items in a list.
I want to display a confirmation prompt before continuing with the delete routine.
I'm not certain how to do this.
I've tried with some code I found online that has a javascript function in the aspx code, but it doesn't seem to process that script.
LVL 67
sirbountyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Can you post the code you tried, or a link to it?
lenamtlCommented:
Hi,

Here the way I do it

function deleteThis(passing value here){
	
	var c = confirm(are_you_sure);
	
	if(c) {
			
		$.ajax({
			type: 'POST',
			your code here....
		},
				
	});
   }	
}

Open in new window

sirbountyAuthor Commented:
lenamtl - web dev newbie here - where would I place that code?
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

sirbountyAuthor Commented:
This is what I'd found, that doesn't seem to fire - it's in my head tag:

     <script type = "text/javascript">
         function DeleteList() {
             var confirm_value = document.createElement("INPUT");
             confirm_value.type = "hidden";
             confirm_value.name = "confirm_value";
             if (confirm("Are you sure you want to delete all selected lists?")) {
                 confirm_value.value = "Yes";
             } else {
                 confirm_value.value = "No";
             }
             document.forms[0].appendChild(confirm_value);
         }
    </script>
lenamtlCommented:
Hi,

What are you deleting exactly?

Usually on confirm alert you have OK and Cancel button
Confirm example
https://www.w3schools.com/jsref/met_win_confirm.asp

Prompt example
https://www.w3schools.com/jsref/met_win_prompt.asp

If you passing form values to ajax then use what I provide earlier.

function deleteThis(passing value here){
	
	var c = confirm(are_you_sure);
	
	if(c) {
			
		$.ajax({
			type: 'POST',
			your code here....
		},
				
	});
   }	
}

Open in new window


If you are not using Ajax and deleting an element or hiding an element
then you can reuse a part of this

var c = confirm(are_you_sure);
	
if(c) { // do something

else { //do nothing

Open in new window


You won't need to assign a yes or no value just pass the code you want to happen if OK is pressed

I have to go now I will be back later
leakim971PluritechnicianCommented:
test page : https://jsfiddle.net/k8ob4ehL/

<form onsubmit="return askIfSure()">
  <ul>
    <li><input type="checkbox" id="cb1" name="cb1" /><label for="cb1">some1</label></li>
    <li><input type="checkbox" id="cb2" name="cb2" /><label for="cb2">some2</label></li>
    <li><input type="checkbox" id="cb3" name="cb3" /><label for="cb3">some3</label></li>
  </ul>
  <input type="submit" value="delete list">
</form>

Open in new window


function askIfSure() {
	var boolAnswer = confirm("are you sure to delete list of item checked?");
  return boolAnswer; // true or false. if false the form is not submitted
}

Open in new window

sirbountyAuthor Commented:
Hi lenamtl - I'm not actually deleting anything from the code.
There will be 3 buttons - create/modify/delete.
The delete will accept one or more selected items from the listbox.
I want to display the item(s) selected, then prompt with "Are you sure?".
If they click no/cancel, nothing happens.
If they click yes - it will write a file to be placed in a queue for system processing.

leakim971 - if I change the form to respond that way, does it require further logic for other buttons?
lenamtlCommented:
Hi,

You need a form /and or a Javascript action (ie onclick) to send the values, then Javascript will display the are you sure message the confirm() will do what you want,
then if yes the data are send (I'm using Ajax) to the server side code let say PHP to do some validations and if this is ok proceed the request if this is not ok display a message to the user (your cannot delete this)...

You may not need a form for example if you have a table and a delete row button you can just pass the values using onclick for that button ID then performed the logical above..
leakim971PluritechnicianCommented:
leakim971 - if I change the form to respond that way, does it require further logic for other buttons?

it look like no. did you check the test page I submited ? the submit button did not change
Obadiah ChristopherDeveloper User InterfaceCommented:
considering that you are using an asp.net button control for the delete button you would use

function DeleteList()

as below:
<script type = "text/javascript">
         function DeleteList() {
             if (confirm("Are you sure you want to delete all selected lists?")) {
                 return true;
             } else {
                 return false;
             }
         }
    </script>

and in your body tag
<asp:Button id="btnDelete" runat="server" OnClientClick="return DeleteList()" />

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sirbountyAuthor Commented:
I've attempted a few different ways, but I'm afraid it's been too long since I've worked with web development.
Obadiah - where would I put this script?  How could I call another function if confirmed?
sirbountyAuthor Commented:
I think I'm going to close this and start with the basics.  I don't want to keep chasing my tail on something I might have to change anyway...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.