Skip to content

Commit

Permalink
Click on checkbox/radio should change state
Browse files Browse the repository at this point in the history
Ref gh-1
Closes gh-21
  • Loading branch information
Lars Laade authored and scottgonzalez committed Jun 5, 2014
1 parent 2b450a9 commit 206bbf8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion jquery.simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ $.extend( $.simulate.prototype, {
},

dispatchEvent: function( elem, type, event ) {
if ( elem.dispatchEvent ) {
if ( elem[ type ] ) {

This comment has been minimized.

Copy link
@AidasK

AidasK Jan 3, 2015

Bug, event object is not passed. Because of this we can not simulate shift click, ctrl click and other custom events.
If 204,205 lines are removed, everything works as expected.

elem[ type ]();
} else if ( elem.dispatchEvent ) {
elem.dispatchEvent( event );
} else if ( elem.fireEvent ) {
elem.fireEvent( "on" + type, event );
Expand Down
5 changes: 5 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ <h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"></div>
<form id="radiocheckbox" class="hidden">
<input id="radiocheckbox-1" type="radio" name="radiocheckbox">
<input id="radiocheckbox-2" type="radio" name="radiocheckbox" checked="checked">
<input id="radiocheckbox-3" type="checkbox" name="radiocheckbox" checked="checked">
</form>
</body>
</html>
5 changes: 5 additions & 0 deletions test/testinit.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
margin: 0;
padding: 0;
border: none;
}

.hidden {
position: absolute;
left: -9999px;
}
25 changes: 25 additions & 0 deletions test/unit/simulate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
(function() {

module( "mouse events" );

test( "click on checkbox triggers change", function() {
var input = $( "#radiocheckbox-3" ),
checked = input.prop( "checked" );

input.simulate( "click" );

notEqual( checked, input.prop( "checked" ), "checkbox state changed" );
});

test( "click on radio triggers change", function() {
var firstRadio = $( "#radiocheckbox-1" ),
secondRadio = $( "#radiocheckbox-2" ),
checked = firstRadio.prop( "checked" );

if ( checked ) {
secondRadio.simulate( "click" );
} else {
firstRadio.simulate( "click" );
}

notEqual( checked, firstRadio.prop( "checked" ), "radio state changed" );
});

var key = jQuery.simulate.keyCode,
keyEvents = [ "keydown", "keyup", "keypress" ],
i = 0;
Expand Down

0 comments on commit 206bbf8

Please sign in to comment.