Please I need your help in this..
I have built a $.blockUI modal and I have an 'X' button to close it.
On button click, if there is some value on textarea I want to display a message that there is input and prevent closing the modal.Any ideas how can I do this?
close_modal function:
function close_modal(event){
$.unblockUI();
if (document.getElementById("comments").value.length > 0){
alert("There is input!");
//Here must be the code to prevent closing modal
.
.
.
//End of code
}
}
You can try something like this
function close_modal(event){
if (document.getElementById("comments").value.length > 0){
if(confirm("There is input!")) $.unblockUI();
} else $.unblockUI();
}
$.unblockUI() is the function to close the modal window. If you won't run it, the modal will stay opened.
function canCloseTheModal() {
return document.getElementById("comments").value.length > 0;
}
function close_modal(event){
if (canCloseTheModal()){
$.unblockUI();
} else {
// inform the user what to do
...
}
}
Related
I want to call a function on one type of button click on my HTML page.
I have got around 10 such button on the page and wrote the below code to call a function when the button is clicked,
$('.divname').each(function (index) {
$(this).on("click", function (e) {
//Call Another Function
});
});
And this is working fine as expected.
However the issue is, sometimes depending on some condition one dynamically generated div is been created(Pop up message) and I dont want my above said code to work when the pop up message comes up.
Could you please advise how this can be achieved.
Thanks and Regards,
Aniket
Hi What something like set a bool and check if it true or false...somthing like:
var enable = true;
$('.divname').each(function (index) {
$(this).on("click", function (e) {
//Call Another Function
if(!enable) return;
});
});
//set it to false on popup show (or something else event)
$(popup).show(function(){
enable = false;
})
In simplest way.
$('.divname').each(function (index) {
$(this).on("click", function (e) {
//Simply check the condition for that Open Pop up message
if(openPopUp){
return;
}
callfunction();
});
});
try something,
$('.divname').each(function (index) {
$(this).on("click", function (e) {
if($('#popupModal:visible').length == 0){ // check length of visible modals
//Call Another Function
}
});
});
first of all, i already trying maybe all of the possible answer in this site but nothing is working for me.
i want to show pop up windows confirmation before delete my data. i use this
Delete
Maybe you need my controller
public function delete($id = 0) {
$id OR redirect(site_url('admin/barang'));
$this->barang_m->delete($id);
redirect(site_url('admin/barang'));
}
My model barang_m
public function __construct(){
parent::__construct();
parent::set_table('barang','idBarang');
}
public function delete($id = 0) {
if($data = parent::get($id)) {
parent::delete($id);
return true;
}
return false;
}
but when i click cancel it still delete the data even when i clicked 'x' to close the confirmation window. i'm so frustated please help me. Or maybe you can tell me why this problem occurs ?
**i alrady tried this possible answer Codeigniter when i click delete i want pop up notification if if click yes or not
jQuery delete confirmation box
Confirm box in CodeIgniter anchor link to delete record
and many more
try
Delete
and your function like this
function isconfirm(url_val){
alert(url_val);
if(confirm('Are you sure ?') == false)
{
return false;
}
else
{
location.href=url_val;
}
}
You could also try
Delete
And js would be
$('.confirmClick').click(()=> {
var sure = confirm('Are you sure ?');
if(sure){
return true;
}
return false;
})
You need to stop page from opening link when you press cancel.
But as seen in code its looks like you missed condition to check so.
Try this may solved you problem.
Delete
<script>
function isconfirm(){
if(!confirm('Are you sure ?')){
event.preventDefault();
return;
}
return true;
}
</script>
It's because you are running it out of the button itself and returning it. So you're saying, delete the button and return the confirmation.
Probably deleting the return will work, or make a function and make sure it doesn't return it but puts what you want in a variable or something.
<a href="javascript:;" onclick="return confirm_Action('<?php echo base_url(); ?>index.php/admin/delete_department/<?php echo $dept['Dept_id']; ?>');" ><button> DELETE</button></a>
Then, Your function confirm_Action could be like this:
//THis Allows Me To Control The Delete Actions
function confirm_Action(url){
// alert(url);
if(confirm('Are You Sure, This Action Can\'t Be Reversed?') == false)
{
return false;
}
else
{
location.href=url;
}
}
just like how #M. Deepak did it...
Thanks... Hope this helps
I have the following href on my site:
a href="http://www.google.com" name="external" onclick="confirmExit(this.name)
I just can't figure out how to make user stay on my website if he/she doesn't want to leave by clicking "cancel" (incomplete code below).
function confirmExit(name){
if(name == "external"){
if(confirm("Go to external site?")){
}
}
}
jQuery/Ajax is not an option.
Just return false.
function confirmExit(name){
if(name == "external"){
if(!confirm("Go to external site?")){
return false;
}
}
}
If you want this to happen on all links, or even when the user closes the tab, check out #megawac's answer.
Use window.onbeforeunload to show a confirm dialog before a user leaves the page
$(window).bind("beforeunload", function() {
if(true) { //display confirm dialog?
return "Are you sure you want to leave?";
}
});
You don't need a function. I suggest you to use this:
$(function () {
$('a[name="external"]').click(function () {
if (!confirm("Go to external site?")) {
event.preventDefault();
}
});
});
If you use this, you don't need to add onclick="confirmExit(this.name)" to everything. Just add the above and your work will be done.
You can do this if you want behaviour of JSFiddle:
window.onbeforeunload = function(){ return "Are you sure you want to leave the page?"; }
Use the return false inside the confirmExit() function and also use return at inline javascript like
HTML
Some Link
//Use return here also ---^
JS
function confirmExit(name){
if(name == "external"){
if(!confirm("Go to external site?")){
return false;
}
}
}
DEMO
Is there away that the confirm box appeared, if i clicked "ok" it will go to another page and if i clicked "cancel" it will just stay and the current page will not reload again? THANK YOU.
function reload()
{
var r=confirm("Do you want to leave page!");
if (r)
{
//write redirection code
window.location = "http://www.yoururl.com";
}
else
{
//do nothing
}
}
call this function when you want to confirmation from user.........
You can use confirm() for this, which returns true on ok or false on cancel.
function myFunction(){
if(confirm("Would you like go to other page?")){
window.location = "http://yahoo.com";
}else{
alert('fine, if not want');
}
}
myFunction();
Updated
DEMO
UPDATED2
<button onclick="return logout()" >logout</button>
<script>
function logout(){
if(confirm("Would you like go to other page?")){
window.location = "failed.php";
}else{
//do your stuff on if press cancel
}
}
</script>
You may try doing
<script>
function myfunction(){
if(confirm("The confirm message")){
youDoTheThingsHere();
return false;
}else{
console.log("I cancelled the dialog!");
return false;
}
}
</script>
I'm not sure about your certain situation and the code that is involved when you call the confirm, but this has worked for me. The other option you may look at as a last resort is using something like a bootstrap modal to trigger a "confirm" modal. With the bootstrap modal you can then style it how you want... hope I could help!
Use "return None"
function myFunction(){
if (confirm("Confirm to reset your content!")){
location.reload();
}else{
return None;
}
}
when you enter text into the status box and click on any hyperlink or click on the back button, a dialog window will display the following message "Are you sure you want to leave this page?"?
Does is this done? how do you do it?
You need to set a dirty flag
<script type="text/javascript">
var isDirty=false;
window.onbeforeunload =function() {
if (isDirty) return "Form not saved";
}
window.onload=function() {
document.getElementById("someFieldId").onblur=function() {
isDirty=this.value.length>0;
}
}
</script>
.
.
.