dododedodonl
dododedodonl

Reputation: 4605

jQuery - anchor click

I've got a working a.click() function in jquery... But if I click an anchor, I open an new window... But how can I stop the browser from opening a new window itself??

example:

    $('a').each(function() {
            $(this).click(function(e) {
                if($(this).attr('target') == '_popup') {
                    //here is code to open window (already exists)

                    //try to stop propagation, but doesn't work...
                    e.stopPropagation();
                }

                hideNSL();
            });
    });

so, how do I stop the event??

Upvotes: 5

Views: 11637

Answers (5)

jpluijmers
jpluijmers

Reputation: 350

EDIT, I stand corrected .preventDefault is not a jQuery function.

Upvotes: -1

Eton B.
Eton B.

Reputation: 6291

e.preventDefault();

Upvotes: 3

MoDFoX
MoDFoX

Reputation: 2134

You could try

e.preventDefault();

Upvotes: 3

Lasse Espeholt
Lasse Espeholt

Reputation: 17792

Try

e.preventDefault()

but here return false may also do it which is in the other answer. preventDefault can be used in more senarios and is a more generic "stop the default event action", see: http://api.jquery.com/event.preventDefault/

Upvotes: 12

Ender
Ender

Reputation: 15221

Add this to the end of your click event:

return false;

So for example:

$('a').each(function() {
        $(this).click(function(e) {
            var toReturn = true;
            if($(this).attr('target') == '_popup') {
                //here is code to open window (already exists)

                toReturn = false;
            }

            hideNSL();
            return toReturn;
        });
});

Upvotes: 4

Related Questions