pixeline
pixeline

Reputation: 17984

livequery - confirm()

I don't get why this confirm() call fires up even though i hit "no". Can you tell me what i'm doing wrong?

$('.deleteButton').livequery('click',function(){
        if(confirm('Are you sure?')){
            return true;
        }else
        {
            return false;
        }
        return false;
    });

The HTML Markup:

<a class="smallbutton deleteButton" href="users.php?todo=delete&id=32">delete</a>

I checked and it does return false, but the page still redirects to the clicked A href value. Normally, if i return false, it shouldn't, right?

Upvotes: 1

Views: 709

Answers (4)

Kaze no Koe
Kaze no Koe

Reputation: 3254

Try this:

$('.deleteButton').livequery('click',function(e){
    if (confirm('Are you sure?')) {
        return true;
    } else {
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
        return false;
    }
});

Upvotes: 0

pixeline
pixeline

Reputation: 17984

I apologize: it turns out i was attaching another behaviour to all anchors inside the main content container. I added an exclusion clause for deleteButton and it works fine now.

Here is my final, working, code:

var $tabs= $("#tabs").tabs({
        fx: {
            opacity: 'toggle'
        },
        load: function(event, ui) { 

            $('a', ui.panel).livequery('click',function() {

                // we need to exclude the listNav buttons AND deleteButton buttons from this behavior

                if($(this).parent().is(':not(".ln-letters")') && $(this).is(':not(".deleteButton")')){

                    $(ui.panel).load(this.href);
                    return false;
                }
            });
        }
    });

And the delete button behaviour works with a simple:

$('.deleteButton').live('click',function(e){
    return confirm('Are you sure?');
});

Upvotes: 0

Willson Haw
Willson Haw

Reputation: 408

Change livequery() to live(). jQuery supports the same functionality. Just tested in FF and it worked with live() but never even gave me a prompt with livequery()

Upvotes: 0

andres descalzo
andres descalzo

Reputation: 14967

<a id="myId_1" href="#" class="deleteButton">delete</a>

$('.deleteButton').livequery('click',function(e){
        e.stopPropagation();
        if(confirm('Are you sure?')){
            functionForDelete($(this).attr("id").split("_")[1]);
        }
});

// OR ir you like goto href

<a id="myId1" href="url/delete/id.php?1" class="deleteButton">delete</a>
$('.deleteButton').livequery('click',function(e){
        e.stopPropagation();
        if(confirm('Are you sure?')){
            window.location=$(this).attr("href");
        }
});

Upvotes: 1

Related Questions