expect(2);
// Supports Fixes bug #7229
try {
- jQuery("#firstp").click(null);
+ jQuery("#firstp").on( "click", null );
ok(true, "Passing a null handler will not throw an exception");
} catch (e) {}
try {
- jQuery("#firstp").click(undefined);
+ jQuery("#firstp").on( "click", undefined );
ok(true, "Passing an undefined handler will not throw an exception");
} catch (e) {}
});
ok( event.data, "bind() with data, check passed data exists" );
equal( event.data["foo"], "bar", "bind() with data, Check value of passed data" );
};
- jQuery("#firstp").bind("click", {"foo": "bar"}, handler).click().unbind("click", handler);
+ jQuery("#firstp").bind("click", {"foo": "bar"}, handler).trigger("click").unbind("click", handler);
ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
var handler2 = function(event) {
equal( event.data, test, "bind() with function data, Check value of passed data" );
};
- jQuery("#firstp").bind("click", test, handler2).click().unbind("click", handler2);
+ jQuery("#firstp").bind("click", test, handler2).trigger("click").unbind("click", handler2);
});
test("click(), with data", function() {
ok( event.data, "bind() with data, check passed data exists" );
equal( event.data["foo"], "bar", "bind() with data, Check value of passed data" );
};
- jQuery("#firstp").click({"foo": "bar"}, handler).click().unbind("click", handler);
+ jQuery("#firstp").on( "click", {"foo": "bar"}, handler).trigger("click").unbind("click", handler);
ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
});
$jq[0].click(); // IE
}
};
- $anchor2.click(function(e) {
+ $anchor2.on( "click", function(e) {
e.preventDefault();
});
$main.delegate("#foo", "click", function(e) {
fakeClick( $anchor2 );
$anchor2.unbind( "click" );
$main.undelegate( "click" );
- $anchor2.click(function(e) {
+ $anchor2.on( "click", function(e) {
// Let the default action occur
});
$main.delegate("#foo", "click", function(e) {
jQuery("div", doc).bind("click", function() {
ok( true, "Binding to element inside iframe" );
- }).click().unbind("click");
+ }).trigger("click").unbind("click");
});
test("bind(), trigger change on select", function() {
}).trigger("tester");
// Make sure events stick with appendTo'd elements (which are cloned) #2027
- jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("#qunit-fixture");
+ jQuery("<a href='#fail' class='test'>test</a>").on( "click", function(){ return false; }).appendTo("#qunit-fixture");
ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
});
};
jQuery("#firstp")
- .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
- .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
+ .bind("click", jQuery.proxy(handler1, thisObject)).trigger("click").unbind("click", handler1)
+ .bind("click", data, jQuery.proxy(handler2, thisObject)).trigger("click").unbind("click", handler2);
ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
});
jQuery( document )
.bind( "click", func )
.unbind( "click", func )
- .click()
+ .trigger("click")
.unbind( "click" );
});
assert( 0 );
});
-test("hover() mouseenter mouseleave", function() {
- expect(1);
+if ( jQuery.fn.hover ) {
+ test("hover() mouseenter mouseleave", function() {
+ expect(1);
- var times = 0,
- handler1 = function( event ) { ++times; },
- handler2 = function( event ) { ++times; };
+ var times = 0,
+ handler1 = function( event ) { ++times; },
+ handler2 = function( event ) { ++times; };
- jQuery("#firstp")
- .hover(handler1, handler2)
- .mouseenter().mouseleave()
- .unbind("mouseenter", handler1)
- .unbind("mouseleave", handler2)
- .hover(handler1)
- .mouseenter().mouseleave()
- .unbind("mouseenter mouseleave", handler1)
- .mouseenter().mouseleave();
+ jQuery("#firstp")
+ .hover(handler1, handler2)
+ .mouseenter().mouseleave()
+ .unbind("mouseenter", handler1)
+ .unbind("mouseleave", handler2)
+ .hover(handler1)
+ .mouseenter().mouseleave()
+ .unbind("mouseenter mouseleave", handler1)
+ .mouseenter().mouseleave();
- equal( times, 4, "hover handlers fired" );
+ equal( times, 4, "hover handlers fired" );
-});
+ });
+}
test("mouseover triggers mouseenter", function() {
expect(1);
var count = 0,
elem = jQuery("<a />");
- elem.mouseenter(function () {
+ elem.on( "mouseenter", function () {
count++;
});
elem.trigger("mouseover");
test("mouseenter, mouseleave don't catch exceptions", function() {
expect(2);
- var elem = jQuery("#firstp").hover(function() { throw "an Exception"; });
+ var elem = jQuery("#firstp").on( "mouseenter mouseleave", function() {
+ throw "an Exception";
+ });
try {
- elem.mouseenter();
+ elem.trigger("mouseenter");
} catch (e) {
equal( e, "an Exception", "mouseenter doesn't catch exceptions" );
}
try {
- elem.mouseleave();
+ elem.trigger("mouseleave");
} catch (e) {
equal( e, "an Exception", "mouseleave doesn't catch exceptions" );
}
});
-test("trigger() shortcuts", function() {
- expect(6);
+if ( jQuery.fn.click ) {
- var elem = jQuery("<li><a href='#'>Change location</a></li>").prependTo("#firstUL");
- elem.find("a").bind("click", function() {
- var close = jQuery("spanx", this); // same with jQuery(this).find("span");
- equal( close.length, 0, "Context element does not exist, length must be zero" );
- ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
- return false;
- }).click();
+ test("trigger() shortcuts", function() {
+ expect(6);
- // manually clean up detached elements
- elem.remove();
+ var elem = jQuery("<li><a href='#'>Change location</a></li>").prependTo("#firstUL");
+ elem.find("a").bind("click", function() {
+ var close = jQuery("spanx", this); // same with jQuery(this).find("span");
+ equal( close.length, 0, "Context element does not exist, length must be zero" );
+ ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
+ return false;
+ }).click();
- jQuery("#check1").click(function() {
- ok( true, "click event handler for checkbox gets fired twice, see #815" );
- }).click();
+ // manually clean up detached elements
+ elem.remove();
- var counter = 0;
- jQuery("#firstp")[0].onclick = function(event) {
- counter++;
- };
- jQuery("#firstp").click();
- equal( counter, 1, "Check that click, triggers onclick event handler also" );
+ jQuery("#check1").click(function() {
+ ok( true, "click event handler for checkbox gets fired twice, see #815" );
+ }).click();
- var clickCounter = 0;
- jQuery("#simon1")[0].onclick = function(event) {
- clickCounter++;
- };
- jQuery("#simon1").click();
- equal( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
+ var counter = 0;
+ jQuery("#firstp")[0].onclick = function(event) {
+ counter++;
+ };
+ jQuery("#firstp").click();
+ equal( counter, 1, "Check that click, triggers onclick event handler also" );
- elem = jQuery("<img />").load(function(){
- ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
- }).load();
+ var clickCounter = 0;
+ jQuery("#simon1")[0].onclick = function(event) {
+ clickCounter++;
+ };
+ jQuery("#simon1").click();
+ equal( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
- // manually clean up detached elements
- elem.remove();
+ elem = jQuery("<img />").load(function(){
+ ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
+ }).load();
- // test that special handlers do not blow up with VML elements (#7071)
- jQuery("<xml:namespace ns='urn:schemas-microsoft-com:vml' prefix='v' />").appendTo("head");
- jQuery("<v:oval id='oval' style='width:100pt;height:75pt;' fillcolor='red'> </v:oval>").appendTo("#form");
- jQuery("#oval").click().keydown();
-});
+ // manually clean up detached elements
+ elem.remove();
+
+ // test that special handlers do not blow up with VML elements (#7071)
+ jQuery("<xml:namespace ns='urn:schemas-microsoft-com:vml' prefix='v' />").appendTo("head");
+ jQuery("<v:oval id='oval' style='width:100pt;height:75pt;' fillcolor='red'> </v:oval>").appendTo("#form");
+ jQuery("#oval").click().keydown();
+ });
+
+}
test("trigger() bubbling", function() {
expect(18);
var form = jQuery("<form action=''></form>").appendTo("body");
// Make sure it can be prevented locally
- form.submit(function(){
+ form.on( "submit", function(){
ok( true, "Local bind still works." );
return false;
});
form.unbind("submit");
- jQuery(document).submit(function(){
+ jQuery(document).on( "submit", function(){
ok( true, "Make sure bubble works up to document." );
return false;
});
$fixture.on( "submit", "form", delegatedSubmit );
// Trigger form submission to introduce the _submit_attached property
- $testForm.on( "submit", noSubmit ).find("input[name=sub1]").click();
+ $testForm.on( "submit", noSubmit ).find("input[name=sub1]").trigger("click");
// Copy the form via .clone() and .html()
$formByClone = $testForm.clone( true, true ).removeAttr("id");
$wrapperDiv.append( $formByClone, $formByHTML );
// Check submit bubbling on the copied forms
- $wrapperDiv.find("form").on( "submit", noSubmit ).find("input[name=sub1]").click();
+ $wrapperDiv.find("form").on( "submit", noSubmit ).find("input[name=sub1]").trigger("click");
// Clean up
$wrapperDiv.remove();
$fixture.on( "change", "form", delegatedChange );
// Trigger change event to introduce the _change_attached property
- $form.find("select[name=select1]").val("1").change();
+ $form.find("select[name=select1]").val("1").trigger("change");
// Copy the form via .clone() and .html()
$formByClone = $form.clone( true, true ).removeAttr("id");
$wrapperDiv.append( $formByClone, $formByHTML );
// Check change bubbling on the copied forms
- $wrapperDiv.find("form select[name=select1]").val("2").change();
+ $wrapperDiv.find("form select[name=select1]").val("2").trigger("change");
// Clean up
$wrapperDiv.remove();
.on( "click", function() {
ok( true, "click fired on p" );
})
- .click()
+ .trigger("click")
.off( "click" )
.end()
.off( "click" )
.on( "click", function( e ){
equal( e.currentTarget, this, "Check currentTarget on event" );
})
- .click()
+ .trigger("click")
.off( "click" )
.end()
.off( "click" );
jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(""); });
jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
- jQuery("#nothiddendiv span").click();
+ jQuery("#nothiddendiv span").trigger("click");
equal( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
equal( livec, 1, "Verify that second handler occurred even with nuked target." );
jQuery("#body").delegate("span#liveSpan1 a", "click", function(){ lived++; return false; });
jQuery("#body").delegate("span#liveSpan1", "click", function(){ livee++; });
- jQuery("span#liveSpan1 a").click();
+ jQuery("span#liveSpan1 a").trigger("click");
equal( lived, 1, "Verify that only one first handler occurred." );
equal( livee, 0, "Verify that second handler doesn't." );
lived = 0;
livee = 0;
- jQuery("span#liveSpan2 a").click();
+ jQuery("span#liveSpan2 a").trigger("click");
equal( lived, 1, "Verify that only one first handler occurred." );
equal( livee, 0, "Verify that second handler doesn't." );
equal( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a delegate handler" );
});
- jQuery("span#liveSpan1 a").click();
+ jQuery("span#liveSpan1 a").trigger("click");
jQuery("#body").undelegate("span#liveSpan1", "click");
equal( ++count, 1, "event called once before removal" );
jQuery().off( event );
})
- .find("a").click().click().end()
+ .find("a").trigger("click").trigger("click").end()
.remove();
});
ok( this.id === "a0_0" , "first li under #u10 was clicked" );
})
.end()
- .find("a").click().end()
+ .find("a").trigger("click").end()
.find("#ul0").off();
// Non-positional selector (#12383)
.on( "click", "li.test a", function() {
ok( true, "li.test is below the delegation point." );
})
- .find("#a0_0").click();
+ .find("#a0_0").trigger("click");
markup.remove();
});
e.stopPropagation();
ok( true, "delegated handler was called" );
})
- .find("a").click().end()
+ .find("a").trigger("click").end()
.remove();
});
ev.preventDefault();
});
- jQuery("#testForm input[name=sub1]").submit();
+ jQuery("#testForm input[name=sub1]").trigger("submit");
equal( count1, 1, "Verify form submit." );
equal( count2, 1, "Verify body submit." );
expect(1);
var markup = jQuery("<div><a href=\"#\" onclick=\"return false\">x</a></div>");
- markup.click(function(e) {
+ markup.on( "click", function(e) {
ok( e.isDefaultPrevented(), "inline handler prevented default");
return false;
});
- markup.find("a").click();
+ markup.find("a").trigger("click");
markup.off("click");
});
test("window resize", function() {
expect(2);
- jQuery(window).unbind();
+ jQuery(window).off();
- jQuery(window).bind("resize", function(){
+ jQuery(window).on( "resize", function(){
ok( true, "Resize event fired." );
- }).resize().unbind("resize");
+ }).trigger("resize").off("resize");
- ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
+ ok( !jQuery._data(window, "events"), "Make sure all the events are gone." );
});
test("focusin bubbles", function() {
.one( "click", 7, function( e, trig ) {
counter += e.data + (trig || 11); // once, 7+11=18
})
- .click()
+ .trigger("click")
.trigger( "click", 17 )
.off( "click" );
equal( counter, 54, "direct event bindings with data" );
counter += e.data + (trig || 11); // once, 7+11=18
})
.find("em")
- .click()
+ .trigger("click")
.trigger( "click", 17 )
.end()
.off( "click", "em" );
.on( "click", "td:last-child", clicked ),
clone = table.clone( true );
- clone.find("td").click();
+ clone.find("td").trigger("click");
equal( counter["center"], 1, "first child" );
equal( counter["fold"], 1, "last child" );
equal( counter["centerfold"], 2, "all children" );
// jQuery click
cb.checked = true;
equal( cb.checked, true, "jQuery - checkbox is initially checked" );
- jQuery( cb ).click();
+ jQuery( cb ).trigger("click");
equal( cb.checked, false, "jQuery - checkbox is no longer checked" );
// Handlers only; checkbox state remains false
expect( 5 );
var $text = jQuery("#text1"),
- $radio = jQuery("#radio1").focus(),
+ $radio = jQuery("#radio1").trigger("focus"),
order;
// IE6-10 fire focus/blur events asynchronously; this is the resulting mess.
// Enabled input getting focus
order = 0;
equal( document.activeElement, $radio[0], "radio has focus" );
- $text.focus();
+ $text.trigger("focus");
setTimeout( function() {
equal( document.activeElement, $text[0], "text has focus" );
ok( true, "Change on original child element is fired" );
});
- fixture.clone().click().change(); // 0 events should be fired
+ fixture.clone().trigger("click").trigger("change"); // 0 events should be fired
clone = fixture.clone( true );
p.off();
checkbox.off();
- p.click(); // 0 should be fired
- checkbox.change(); // 0 should be fired
+ p.trigger("click"); // 0 should be fired
+ checkbox.trigger("change"); // 0 should be fired
- clone.find("p:first").trigger( "click", true ); // 3 events should fire
+ clone.find("p:first").trigger( "click", true ); // 3 events should fire
clone.find("#check1").trigger( "change", true ); // 3 events should fire
clone.remove();
- clone.find("p:first").click(); // 0 should be fired
- clone.find("#check1").change(); // 0 events should fire
+ clone.find("p:first").trigger("click"); // 0 should be fired
+ clone.find("#check1").trigger("change"); // 0 events should fire
});
test( "Check order of focusin/focusout events", 2, function() {
});
// gain focus
- input.focus();
+ input.trigger("focus");
// then lose it
- jQuery("#search").focus();
+ jQuery("#search").trigger("focus");
// cleanup
input.off();
equal( result.text(), defaultText, "Check for element wrapping" );
QUnit.reset();
- jQuery("#check1").click(function() {
+ jQuery("#check1").on( "click", function() {
var checkbox = this;
ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
equal( j[ 0 ].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
// Wrap an element with a jQuery set and event
- result = jQuery("<div></div>").click(function() {
+ result = jQuery("<div></div>").on( "click", function() {
ok( true, "Event triggered." );
// Remove handlers on detached elements
$radioChecked = jQuery("input:radio[name='R1']").eq( 1 );
$radioParent = $radioChecked.parent();
$radioUnchecked = jQuery("<input type='radio' name='R1' checked='checked'/>").appendTo( $radioParent );
- $radioChecked.click();
+ $radioChecked.trigger("click");
$radioUnchecked[ 0 ].checked = false;
$radioParent.wrap("<div></div>");
equal( $radioChecked[ 0 ].checked, true, "Reappending radios uphold which radio is checked" );
t( "Append select", "#foo select", [ "select1" ] );
QUnit.reset();
- div = jQuery("<div/>").click(function() {
+ div = jQuery("<div/>").on( "click", function() {
ok( true, "Running a cloned click." );
});
div.appendTo("#qunit-fixture, #moretests");
- jQuery("#qunit-fixture div:last").click();
- jQuery("#moretests div:last").click();
+ jQuery("#qunit-fixture div:last").trigger("click");
+ jQuery("#moretests div:last").trigger("click");
QUnit.reset();
div = jQuery("<div/>").appendTo("#qunit-fixture, #moretests");
deepEqual( jQuery("#anchor1").contents().get(), [ tmp ], "Replace text node with element" );
- tmp = jQuery("<div/>").appendTo("#qunit-fixture").click(function() {
+ tmp = jQuery("<div/>").appendTo("#qunit-fixture").on( "click", function() {
ok( true, "Newly bound click run." );
});
- y = jQuery("<div/>").appendTo("#qunit-fixture").click(function() {
+ y = jQuery("<div/>").appendTo("#qunit-fixture").on( "click", function() {
ok( false, "Previously bound click run." );
});
- child = y.append("<b>test</b>").find("b").click(function() {
+ child = y.append("<b>test</b>").find("b").on( "click", function() {
ok( true, "Child bound click run." );
return false;
});
y.replaceWith( val(tmp) );
- tmp.click();
- y.click(); // Shouldn't be run
- child.click(); // Shouldn't be run
+ tmp.trigger("click");
+ y.trigger("click"); // Shouldn't be run
+ child.trigger("click"); // Shouldn't be run
- y = jQuery("<div/>").appendTo("#qunit-fixture").click(function() {
+ y = jQuery("<div/>").appendTo("#qunit-fixture").on( "click", function() {
ok( false, "Previously bound click run." );
});
- child2 = y.append("<u>test</u>").find("u").click(function() {
+ child2 = y.append("<u>test</u>").find("u").on( "click", function() {
ok( true, "Child 2 bound click run." );
return false;
});
y.replaceWith( val(child2) );
- child2.click();
+ child2.trigger("click");
set = jQuery("<div/>").replaceWith( val("<span>test</span>") );
equal( jQuery("#nonnodes").contents().clone().length, 3, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
// Verify that clones of clones can keep event listeners
- div = jQuery("<div><ul><li>test</li></ul></div>").click(function() {
+ div = jQuery("<div><ul><li>test</li></ul></div>").on( "click", function() {
ok( true, "Bound event still exists." );
});
clone = div.clone( true ); div.remove();
// Verify that cloned children can keep event listeners
div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
- div.find("table").click(function() {
+ div.find("table").on( "click", function() {
ok( true, "Bound event still exists." );
});
clone.remove();
// Make sure that doing .clone() doesn't clone event listeners
- div = jQuery("<div><ul><li>test</li></ul></div>").click(function() {
+ div = jQuery("<div><ul><li>test</li></ul></div>").on( "click", function() {
ok( false, "Bound event still exists after .clone()." );
});
clone = div.clone();
count = 0;
first = jQuery("#ap").children(":first");
- cleanUp = first.click(function() {
+ cleanUp = first.on( "click", function() {
count++;
- }).remove().appendTo("#qunit-fixture").click();
+ }).remove().appendTo("#qunit-fixture").trigger("click");
strictEqual( 0, count, "Event handler has been removed" );
count = 0;
first = jQuery("#ap").children(":first");
- cleanUp = first.click(function() {
+ cleanUp = first.on( "click", function() {
count++;
- }).detach().appendTo("#qunit-fixture").click();
+ }).detach().appendTo("#qunit-fixture").trigger("click");
strictEqual( 1, count, "Event handler has not been removed" );
div.remove();
function getDiv() {
- var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function() {
+ var div = jQuery("<div class='outer'><div class='inner'></div></div>").on( "click", function() {
ok( true, type + " " + pos + " Click event fired." );
- }).focus(function() {
+ }).on( "focus", function() {
ok( true, type + " " + pos + " Focus event fired." );
- }).find("div").click(function() {
+ }).find("div").on( "click", function() {
ok( false, type + " " + pos + " Click event fired." );
- }).focus(function() {
+ }).on( "focus", function() {
ok( false, type + " " + pos + " Focus event fired." );
}).end().appendTo("body");