10

I have this code:

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $('a.one').click(function(event){
                event.preventDefault();
            });
        });
        function test(event){
            event.preventDefault();
        }
    </script>
    <style type="text/css">
        a.test { font-weight: bold; }
        body { font-family:sans-serif; background-color:#AAAAAA;}
    </style>
</head>
<body>
    <a class="one" href="http://jquery.com/">jQuery</a>
    <br/>
    <a class="two" href="http://stackoverflow.com/" onclick='test(event)'>stack overflow</a>
</body>
</html>

The test-function does not work as it stands now, since a regular javascript event doesn't support the jQuery event preventDefault-function. Is there some way to wrap a regular javascript event in a jQuery event so that I can use e.g. preventDefault?

5 Answers 5

12

Try this:

function test(e) {
    $.Event(e).preventDefault();
}

Event object

Sign up to request clarification or add additional context in comments.

2 Comments

This does give you access to some methods, but is not a "normal" JQuery event... For instance, you cannot use: $.Event(e).which
$.Event(e) does implement preventDefault, stopPropagation, and stopImmediatePropagation. github.com/jquery/jquery/blob/master/src/event.js#L664
5

I've found the best way to wrap a native event in a jQuery event is with fix:

event = $.event.fix(event);

Please note, this function is not part of the public API (although it really should be).

Comments

0

I think it may be the fact that you're passing event in with onclick='test(event)'. I think onclick='test' is enough. I could be wrong though.

1 Comment

It's fine. It picks up the event global on IE and the event argument on other browser and passes it to the function so that it doesn't have to do the usual if (!e) e= window.event IE workaround.
0

Yes (see Darin's answer). You could also work around IE's lack of preventDefault instead (which is essentially what jQuery is doing):

if ('preventDefault' in event)
    e.preventDefault();
else
    e.returnValue= false;

Comments

0

When you just want to execute the javascript - and not redirect - when clicking the href use "return false" in your click function. For example:

$(function(){
        $('a.one').click(function(event){
                var condition = confirm('Do you want to redirect to ...?');
                return condition == true;
        });
});

If you never want the link to redirect use 'javascript:void(0);' as href attribute, all browsers will still render it as a link instead of an anchor (some IE version do this).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.