Skip to content

Commit 206bbf8

Browse files
Lars Laadescottgonzalez
authored andcommitted
Click on checkbox/radio should change state
Ref jquerygh-1 Closes jquerygh-21
1 parent 2b450a9 commit 206bbf8

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

jquery.simulate.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ $.extend( $.simulate.prototype, {
201201
},
202202

203203
dispatchEvent: function( elem, type, event ) {
204-
if ( elem.dispatchEvent ) {
204+
if ( elem[ type ] ) {
205+
elem[ type ]();
206+
} else if ( elem.dispatchEvent ) {
205207
elem.dispatchEvent( event );
206208
} else if ( elem.fireEvent ) {
207209
elem.fireEvent( "on" + type, event );

test/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@ <h2 id="qunit-banner"></h2>
1919
<h2 id="qunit-userAgent"></h2>
2020
<ol id="qunit-tests"></ol>
2121
<div id="qunit-fixture"></div>
22+
<form id="radiocheckbox" class="hidden">
23+
<input id="radiocheckbox-1" type="radio" name="radiocheckbox">
24+
<input id="radiocheckbox-2" type="radio" name="radiocheckbox" checked="checked">
25+
<input id="radiocheckbox-3" type="checkbox" name="radiocheckbox" checked="checked">
26+
</form>
2227
</body>
2328
</html>

test/testinit.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
margin: 0;
88
padding: 0;
99
border: none;
10+
}
11+
12+
.hidden {
13+
position: absolute;
14+
left: -9999px;
1015
}

test/unit/simulate.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
(function() {
22

3+
module( "mouse events" );
4+
5+
test( "click on checkbox triggers change", function() {
6+
var input = $( "#radiocheckbox-3" ),
7+
checked = input.prop( "checked" );
8+
9+
input.simulate( "click" );
10+
11+
notEqual( checked, input.prop( "checked" ), "checkbox state changed" );
12+
});
13+
14+
test( "click on radio triggers change", function() {
15+
var firstRadio = $( "#radiocheckbox-1" ),
16+
secondRadio = $( "#radiocheckbox-2" ),
17+
checked = firstRadio.prop( "checked" );
18+
19+
if ( checked ) {
20+
secondRadio.simulate( "click" );
21+
} else {
22+
firstRadio.simulate( "click" );
23+
}
24+
25+
notEqual( checked, firstRadio.prop( "checked" ), "radio state changed" );
26+
});
27+
328
var key = jQuery.simulate.keyCode,
429
keyEvents = [ "keydown", "keyup", "keypress" ],
530
i = 0;

0 commit comments

Comments
 (0)