Solved

Determine which button pressed in Bootbox prompt

Posted on 2016-10-05
8
49 Views
Last Modified: 2016-10-06
I'm using a bootbox prompt to allow a user to update a field or indicate if it should be deleted.  Unfortunately I can't work out how to determine which button has been pressed.

bootbox.prompt({
	title: "Edit Custom Product Catalog",
	value: 'Default Text',
	buttons: {
		cancel: {
			label: 'Delete Catalog',
			className: 'btn-danger',
			callback: function () {
				alert('Cancel Button Pressed');
			}
		},
		confirm: {
			label: 'Save',
			className: 'btn-success',
			callback: function () {
				alert('Save Button Pressed');
			}
		}
	},
	callback: function (result) {
		alert('This always called');
	}
});

Open in new window


https://jsfiddle.net/scotie/0az7ccc2/1/https://jsfiddle.net/scotie/0az7ccc2/1/
0
Comment
Question by:canuckconsulting
8 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41830362
You can bind a regular click event listener:

$('.btn-success, .btn-danger').on('click',function() {
      alert($(this).text());
});

Updated Fiddle: https://jsfiddle.net/zephyr_hex/roy7umkf/2/
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41830368
At what point do you need to determine which button was clicked? They each have separate callback functions, so you shouldn't have any trouble determining this on the front end. If you give them a name and a value, you should be able to determine which button is pressed on the backend after the form is submitted.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41830379
Look at the docs for Prompt

callback: function (result) {
  // If null then cancelled
  if (result === null) {
    alert('null');
  }
  else {
    alert('not null');
  }
}

Open in new window

0
 

Author Comment

by:canuckconsulting
ID: 41830810
Hi guys, and thanks for the responses.

zephyr_hex - thanks...uglier than I'd hoped but does the trick given the apparent limitation of Fiddler.  Cheers!

Kim Walker - I should have noted neither of the button callbacks are working.  This can be verified in the jsfiddle.

Julian Hansen - This does not tell me if the Delete was pressed or if the dialog was closed by virtue of clicking the close X.   I should have added a third button to make my situation clearer.  I need to know what button is clicked.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41831240
Null = delete
Not Null = save
Let me re-phrase the sample
callback: function (result) {
  if (result==null) {
    alert('Delete');
  }
  else {
    alert('Save');
  }
}

Open in new window


Updated fiddle https://jsfiddle.net/9st093a8/
0
 

Author Comment

by:canuckconsulting
ID: 41831439
Hi Julian and thanks for the follow up.

Unfortunately this does not tell me if the Delete was pressed or if the dialog was closed by virtue of clicking the close X:

gui
0
 

Author Closing Comment

by:canuckconsulting
ID: 41831445
This worked great..thanks!

In the end I moved to using the custom dialog functionality which seemed to be a better fit for the task:

https://jsfiddle.net/scotie/9st093a8/1/

 bootbox.dialog({
        message: '<div class="form-group">' +
                    '<input id="CustomCatalogName" type="text" class="form-control" value="Default Text" autocomplete="off"></div>',
        buttons: {
            confirm: {
                label: 'Save',
                className: 'btn-success',
                callback: function () {
                      alert('save');
                    }
                },
            cancel: {
                label: 'Delete',
                className: 'btn-danger',
                callback: function () {
                   alert('delete');
                }
            }
        }
    });
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41831450
Begs the question as to why you are using bootbox in the first place. Essentially you are back to a customised Bootstrap modal
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now