Welcome Guest, Not a member yet? Register   Sign In
jQuery Delete Confirmation
#1

[eluser]Kemik[/eluser]
Hello,

I have a user list and want to create a pop-up asking the admin to confirm if they want to delete the user when they click on a link.

It seems jQuery is a popular Javascript library so I'm looking for something based on that. I've looked around google and the jQuery site for some sample code but haven't had much luck.

If anyone knows any good resources I'd appreciate it.

Thanks.
#2

[eluser]Alex007[/eluser]
Why not just a simple Javascript Confirm() ?

My "delete" link looks like this:
Code:
&lt;a href="#" onclick="return deletechecked();"&gt;Delete checked messages</a>

Which points to this javascript function:
Code:
function deletechecked()
{
    var answer = confirm("Delete selected messages ?")
    if (answer){
        document.messages.submit();
    }
    
    return false;  
}

Which submits the form, and the messages are deleted.
#3

[eluser]xwero[/eluser]
do you want the common modal window. The only thing you need is a class
Code:
$('.delete').click(function(){
  var answer = confirm('Delete user');
  return answer // answer is a boolean
});
If you want a full javascripted modal window check the modal plugins like blockUI
#4

[eluser]Kemik[/eluser]
Thanks guys.

To pass a variable could I just do:
Code:
&lt;a href="#" onclick="return deletechecked('Are you sure you want to delete &lt;?=$row-&gt;username ?&gt;');">Delete checked messages</a>
Javascript
Code:
function deletechecked(message)
{
    var answer = confirm(message)
    if (answer){
        document.messages.submit();
    }
    
    return false;  
}
#5

[eluser]Unknown[/eluser]
Hi,

I'm new to CodeIgniter and I like it a lot.

[quote author="xwero" date="1201829209"]do you want the common modal window. The only thing you need is a class
Code:
$('[b].delete[/b]').click(function(){
  var answer = confirm('Delete user');
  return answer // answer is a boolean
});

[/quote]

Thank you xwero.
Using jQuery, I added in CI a class to anchor and it worked.
Code:
echo "<td>". anchor('controller/delete/'.$row->id,'Delete',array('class' => 'delete')) ."</td>";
The drawback is that is doesn't show witch record removes.Maybe adding a 'title' to the anchor then showing this in javascript code could do it.

The good is that jQuery is unobtrusive.

PS: I figured out how to get with jQuery anchor attributes

jQuery function
Code:
$(document).ready(function(){
        $('.delete').click(function(){
            var answer = confirm('Delete '+jQuery(this).attr('title'));
                        // jQuery(this).attr('title') gets anchor title attribute
            return answer; // answer is a boolean
            });
    });

anchor in ci view file
Code:
echo "<td>". anchor('controller/delete/'.$row->id,'Delete',array('class' => 'delete', 'title'=>"$row->username")) ."</td>";
#6

[eluser]Dave Rau[/eluser]
Hey guys, I'm doing something similar but I'd like to use better URLs instead of just href="#", anyone have any advice on that?

I tried jquery confirm, but I can't get my parent TR to hide after I OK the delete:
Code:
$('a.delete').click(function(event) {

    $.post($(this).attr('href'), function(data){
        $(this).parent('tr').fadeOut('fast');
    });

}).confirm();

anyone have any thoughts?
#7

[eluser]cahva[/eluser]
You can use any href as you wish. You just have to return false always. Taken from example earlier:

Code:
function deletechecked(message)
{
    var answer = confirm(message)
    if (answer){
        document.messages.submit();
        return false; // This line added
    }
    
    return false;  
}
#8

[eluser]Dave Rau[/eluser]
Right on, thanks cahva!
#9

[eluser]cahva[/eluser]
Actually that line is not needed in that particular example because that indeed returns false always even if the confirm was cancelled(return false is outside the if). My bad. But anyways, the return always false was the point Smile
#10

[eluser]Unknown[/eluser]
Hi Guys i'm New to this CodeIgniter that delete conformation is not working for me it is firing before i delete any record(i gave a simple alert in it).i wrote that code in the same view page is that correct please reply fast

this is my code
Code:
[removed]
alert("hai");
$(document).ready(function(){
        $('.delete').click(function(){
            var answer = confirm('Delete '+jQuery(this).attr('title'));
                        // jQuery(this).attr('title') gets anchor title attribute
            return answer; // answer is a boolean
            });
    });
[removed]
Code:
echo "<td >".anchor('all/delete/'.$row->newsid, 'Delete', array('class' => 'delete', 'title'=>"$row->newsid"))."</td>";




Theme © iAndrew 2016 - Forum software by © MyBB