Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I wrap a javascript event in a jQuery event?

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?

like image 648
Cros Avatar asked Nov 25 '09 10:11

Cros


People also ask

What does the jQuery wrap () function do?

jQuery wrap() method is used to wrap specified HTML elements around each selected element. The wrap () function can accept any string or object that could be passed through the $() factory function.

What is wrap and unwrap in jQuery?

createElement() to create an element, and wrap it around each selected element. Wrap elements using a function. Using a function to specify what to wrap around each selected element. Wrap and unwrap an element. How to toggle between wrapping and unwrapping an element.

What is wrapped set in jQuery?

The wrapped set is simply a list of DOM elements(with their children) in the order in which they are defined in the current document that matches a selector or in the order in which they have been created on the fly with the $(html) function.

Which jQuery method allows you to add a wrapper element around another set of elements?

wrap( wrappingElement )Returns: jQuery. Description: Wrap an HTML structure around each element in the set of matched elements.


1 Answers

Try this:

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

Event object

like image 156
Darin Dimitrov Avatar answered Oct 24 '22 07:10

Darin Dimitrov