tags:

views:

236

answers:

2

Hi guys,

I read that the new delegate method can attach more than one event to a handler however I have not see this in practice. Would anyone type a code for this please.

I tried:

$("body").delegate("input", "mouseup", "mouseover", function() {
            $(this).after("<p>Another paragraph!</p>");
        });

and it appended the paragraph only on mouseup. I am confused.

+2  A: 

Use a space delimiter for events like this:

$("body").delegate("input", "mouseup mouseover", function() {
   $(this).after("<p>Another paragraph!</p>");
});

You probably meant to select <input> elements, I changed this above, correct me if you meant otherwise.

Alternatively, use .bind() like this:

$("body input").bind("mouseup mouseover", function() {
   $(this).after("<p>Another paragraph!</p>");
});

I find this easier to read, but whatever floats your boat.

Nick Craver
(types || "").split is not a function
XGreen
yes (comment nuked) :-)
Pointy
@XGreen - Fixed, forgot you're not binding, which is simpler in this case, I'll add an example
Nick Craver
oh yes. sorry I make a mistake in typing. Magic. thanks
XGreen
A: 

The reference (http://api.jquery.com/delegate/) says about second parameter:

A string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names.

Therefore, in your case:

$("body").delegate("input", "mouseup mouseover", function() {
            $(this).after("<p>Another paragraph!</p>");
        });
Marko Dumic