Checking OK button is clicked on alert box - javascript

This the code for alerting some value:
alert('Click the OK button Now !');
So now i want to check whether the OK button is clicked or not.
How can I do this using this JavaScript?

Confirm could work:
var r=confirm("Click the OK button now!");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
Confirm HAS to have an OK and Cancel button. If you only want one button, you should either use the alert() method (which doesn't tell you if the OK was clicked) or you should look into something like the jQueryUI Dialog control.
The jQueryUI dialog is a bit more complicated because you need to include some extra JavaScript libraries and do a bit of extra wiring up to get it to work. There are a lot of examples to follow here.

Sounds like you might want a confirm box instead of an alert:
http://www.w3schools.com/js/js_popup.asp
This returns true or false depending on what the user presses. Alert does not return a value.

Use a jquery dialog then you can post or check what every you want from a range of buttons. Much more flexible
Jquery Modal

As mentioned previously, confirm() is the best bet, however don't forget you can check which button was pressed, and ask for a value at the same time using prompt().
if (prompt("Click the OK button?")!=null)
{
alert('you clicked OK and entered a value')
}
else
{
alert('you clicked cancel')
}

Related

I want to show a div after pressing on submit button

I need some help
The "close modal" button that you see in the line below is hidden at load because i want to force users to write their names and emails before accessing the content.
<div class="mc-closeModal" data-action="close-mc-modal" data-dojo-attach-point="modalClose" style="">close</div>
What i want to do now is to make it appear again after people have entered their names and emails and pressed on the "Subscribe" button.
I've tried this code below in the console and it works. It submits the details and makes the "close" button appear again.
$(document).ready(function() {
$('iframe').contents().find('input.button').click() //this line targets the submit button "Subscribe"
$("#PopupSignupForm_0 > div.mc-modal > div.mc-closeModal").show() //this line makes the "close" button appear again
});
The problem is that it doesn't work when i add it in the website though.
What i want is pretty simple
When this event happens
$('iframe').contents().find('input.button').click()
I want this to execute
$("#PopupSignupForm_0 > div.mc-modal > div.mc-closeModal").show()
Your code is just two statments one after another. You're not actually binding anything to the click event. If you wanted to use jquery's click you could do something like this:
$('iframe').contents().find('input.button').click(function() {
$("#PopupSignupForm_0 > div.mc-modal > div.mc-closeModal").show()
});
But then you have no idea if the user actually filled in the form. Instead you can
try this:
$('#content__formFields').submit(function() {
var isValid = true;
// Check if empty of not
$(this).find('input[type!="hidden"]').each(function() {
if (!$(this).val()) {
isValid = false;
//change the element's css here so user knows whats missing
}
});
if (isValid)
$("#PopupSignupForm_0 > div.mc-modal > div.mc-closeModal").show()
return isValid
});

Passing confirmation to an alert in java script

I am writing a function to delete some records. on deletion there is an alert message with 'yes' and 'no' to which user needs to click to delete the record.
The problem is that I want to pass yes to this confirmation without manually clicking on the yes. how can I do this. please provide pointers.
Not saying you should do the following, but here's how one might try to bypass confirmation dialogs.
Replace the confirm function with your own function that returns true.
var realConfirm = window.confirm;
window.confirm = function () { return true; }
deleteRows();
window.confirm = realConfirm;
If you are talking about native javascript's confirm() then, you cannot do what you intend to. If you override the function, you'll not get the popup behaviour.
But if you have a custom alert box kind of thing, then assign an id to the Yes or Ok button.
Then you can do this:
document.getElementById('yesId').click(); //this will trigger a click.
And yes, as Will Newton said, if you don't wanna press the button, just use Return or Enter button.

Form validation javascript hiding elements

I am building a form and alerting the user that they can not move to the next step until the input is successful
var location=document.getElementById("zip").value;
if(location==null || location==""){
alert("Please insert a valid location!");
return false;
}
If the user continues to the next step, the submit button is activated. If the user returns to this step, I want to hide the submit button only on this step if the same input is null. How can I do this using only javascript? The class of the submit button is 'btFinish'.
Thanks!
EDIT
I am using jQuery. I have tried
$(btPrevious).click(function(e) {
$(btFinish).hide();
});
$(btNext).click(function(e) {
$(btFinish).show();
});
This hides the Finish button through the progression, but if there is an error in the input box, and the user clicks 'btNext', 'btFinish' will show even if the user must fix their input on the first step.
ok so from my understanding here you have two steps:
1) Zip must be filled in then user clicks to go to next step.
2) User can either submit finished or go back to the zip code step.
I would do something like this:
$(btPrevious).click(function(e) {
$(btFinish).hide();
});
$(btNext).click(function(e) {
$(btFinish).show();
});
$('#zip').keyup(function(){
if ($(this).val() === null || $(this).val() === ""){
$(btFinish).hide();
}
else{
$(btFinish).show();
}
});
this will prevent them from clicking on btFinish unless they have something written in the zip. I'm thinking that since they would hve to press the delete button, it should trigger the event handler. In the event that they can somehow elsewise clear the zip code, you could also check on blur or for whatever other event they can clear the box with.
I would suggest you are trying something like that:
var location = document.formname.inputname.value;
if(location == ""){
alert("No Valid Location")
}

Is there a way to capture the alert ok button click event?

Is there a way to capture the alert ok button click event? In jQuery?
The alert() function is synchronous and you can't verify what was clicked (it does not return anything), so the code below the call will be executed after it is closed (ok or close button). The alert is not used to gain user input. It is an alert, a message to the user. If you need to check what the user want, you should use confirm(). Note that the function name tells its purpose like alert.
Something like:
// if the ok button is clicked, result will be true (boolean)
var result = confirm( "Do you want to do this?" );
if ( result ) {
// the user clicked ok
} else {
// the user clicked cancel or closed the confirm dialog.
}
Alert is a blocking function, means, if you don't close it, the code below will not execute.
So you don't have to capture the alert close event, just write down the code below that alert, when alert window will be closed the code below will be executed automatically.
See example below:
alert("Close Me");
// Write down the code here, which will executed only after the alert close
console.log("This code is executed after alert")
Disclaimer: This is a very bad thing to do.
Technically you could hook into it with this code:
window.alert = function(al, $){
return function(msg) {
al(msg);
$(window).trigger("okbuttonclicked");
};
}(window.alert, window.jQuery);
$(window).on("okbuttonclicked", function() {
console.log("you clicked ok");
});
alert("something");
​
Demo: http://jsfiddle.net/W4d7J/1/
There is no event for the window.alert(). Basically the next line after it is called when they click ok. I am not sure why you would need to listen for it.
I tried this in a site I created and it worked perfectly :
<< Back
You could use JAlert and assign a click handler to the ok button.
Something like
jAlert("Alert message goes here.");
$('#popup_ok').bind('click',function(){
//Do operation after clicking ok button.
function_do_operation();
});

Detecting form changes using jQuery when the form changes themselves were triggered by JS

I have a list of radio buttons that I can toggle "yes" or "no" to using Javascript.
$(document).ready(function(){
$('#select-all').click(function(){
$('#notifications .notif-radio').each(function(){
$('input[type="radio"]', this).eq(0).attr('checked', true);
$('input[type="radio"]', this).eq(1).attr('checked', false);
});
});
$('#deselect-all').click(function(){
$('#notifications .notif-radio').each(function(){
$('input[type="radio"]', this).eq(0).attr('checked', false);
$('input[type="radio"]', this).eq(1).attr('checked', true);
});
});
});
this works just fine. Now I have a separate piece of code that detects when a user has changed something, and asks them if they want to leave the page.
var stay_on_page;
window.onbeforeunload = confirm_exit;
$('.container form input[TYPE="SUBMIT"]').click(function(){
stay_on_page = false;
});
$('#wrapper #content .container.edit-user form').change(function(){
stay_on_page = true;
});
function confirm_exit()
{
if(stay_on_page){ return "Are you sure you want to navigate away without saving changes?"; }
}
The problem is that if the user uses the first piece of functionality to toggle all radio buttons one way or another. The JS detecting form changes doesn't see that the form was changed. I have tried using .live, but to no avail. Anyone have any ideas?
I do something similar to this by adding change() (or whatever's appropriate, click() in your case I suppose) event handlers which set either a visible or hidden field value, then check that value as part of your onbeforeunload function.
So, my on before unload looks like:
window.onbeforeunload = function () {
if ($('#dirtymark').length) {
return "You have unsaved changes.";
}
};
And, or course, dirtymark is added to the page (a red asterisk near the Save button), when the page becomes dirty.

Categories