4 <title>Event Delegation Tests
</title>
5 <script src=
"jquery.js"></script>
8 border-collapse: collapse;
21 border:
1px solid #bbb;
26 <h2>Delegate Tests (
<span id=
"fileversion">x
</span>)
</h2>
36 <option value='one1'
>one1
</option>
37 <option value='one2'
>one2
</option>
38 <option value='one3'
>one3
</option>
41 <option value='two1'
>two1
</option>
42 <option value='two2'
selected=
"selected">two2
</option>
43 <option value='two3'
>two3
</option>
47 <select multiple=
"multiple">
48 <option value='multi1'
>multi1
</option>
49 <option value='multi2'
>multi2
</option>
50 <option value='multi3'
>multi3
</option>
54 <input type=
"checkbox" name=
"mycheckbox" id=
"check1"/>
55 <label for=
"check1">check1
</label><br/>
56 <input type=
"checkbox" name=
"mycheckbox" id=
"check2"/>
57 <label for=
"check2">check2
</label><br />
58 <input type=
"checkbox" name=
"mycheckbox" id=
"check3" disabled=
"disabled"/>
59 <label for=
"check3">check3
</label>
62 <input type=
"radio" name=
"myradio" id=
"radio1"/>
63 <label for=
"radio1">Radio1
</label><br/>
64 <input type=
"radio" name=
"myradio" id=
"radio2"/>
65 <label for=
"radio2">Radio2
</label><br />
66 <input type=
"radio" name=
"myradio" id=
"radio3" disabled=
"disabled"/>
67 <label for=
"radio3">Radio3
</label>
70 <input class=
"file_test" id=
"file1" type=
"file"/>
73 <input class='test' value='' id='input' size='
10'
/>
74 <input class='test' value='test' id='input2' size='
10'
readonly=
"readonly" />
77 <textarea rows='
2'
></textarea>
80 <button name=
"mybutton1" id=
"button1">Button
</button><br />
81 <button name=
"mybutton2" id=
"button2"><span>Button w/ child
</span></button><br />
82 <button name=
"mybutton3" id=
"button3" disabled=
"disabled">Button Disabled
</button><br />
83 <button name=
"mybutton4" id=
"button4" disabled=
"disabled"><span disabled=
"disabled">Button, child Disabled
</span></button><br />
90 <p>NOTE: Only IE supports propertychange, beforeactivate, beforedeactivate; buttons do not support change events.
</p>
99 <form action=
"" id=
"text_submit">
100 <input class='test' type='text' value='Key Return To Submit'
/>
104 <form action=
"" id=
"password_submit">
105 <input class='test' type='password' value=''
/>
109 <form action=
"" id=
"submit_submit">
110 <input type='submit'
value=
"Click Me To Submit" />
113 <td>$(document).bind('submit')
</td>
117 <td id='textSubmit'
class=
"red">TEXT
</td>
118 <td id='passwordSubmit'
class=
"red">PASSWORD
</td>
119 <td id='submitSubmit'
class=
"red">BUTTON
</td>
120 <td id='boundSubmit'
class=
"red">DOCUMENT
</td>
124 <form id=
"autosub"><input type=submit name=subme
/></form>
126 <script type='text/javascript'
>
128 $("#fileversion").text($.fn
.jquery
);
130 // Try an auto-submit, it should only fire once
132 var triggered
= false;
133 $("#autosub input").trigger("keypress");
134 $("body").on("submit", "#autosub", function( e
){
138 alert("autosubmit FAIL");
142 $("#autosub").submit().remove();
145 // Events we want to track in row-order
146 var events
= "bind-change live-change onX-change bind-propertychange live-beforeactivate live-focusin bind-focus live-beforedeactivate live-focusout bind-blur live-click live-keydown".split(" "),
148 blinker = function(event
){
150 $("#changes tbody td").text("");
152 var $el
= event
.data
,
154 prev
= prev
? prev
+" | " : "";
156 .text(prev
+ ++counter
+" " + (this.value
.replace(/^on$/,"") || this.id
|| this.checked
|| ""))
157 .css("backgroundColor","#0f0")
159 .queue(function(next
){
160 $el
.css("backgroundColor","#afa");
166 for ( var i
=0; i
< events
.length
; i
++ ) {
167 var m
= events
[i
].split("-"),
170 $row
= $("<tr><th>"+type
+" "+api
+"</th></tr>");
172 $("#changes thead td").each(function(){
173 var id
= "#"+this.id
,
174 $cell
= $('<td></td>');
175 if ( api
== "onX" ) {
176 $(this).find("input, button, select, textarea").each(function(){
177 this["on"+type
] = function(e
){ e
= $.event
.fix(e
||event
); e
.data
= $cell
; blinker
.call(this, e
); };
179 } else if ( api
== "bind" ) {
180 $(this).find("input, button, select, textarea").bind(type
, $cell
, blinker
);
182 $(id
+" input,"+id
+" button,"+id
+" select,"+id
+" textarea").live(type
, $cell
, blinker
);
186 $("#changes tbody").append($row
);
189 // Ensure that cloned elements get the delegated event magic; this is
190 // implementation-specific knowledge but otherwise impossible to test.
191 // The beforeactivate event attaches a direct-bound change event.
192 // (Only care about the live change for this third select element.)
193 var sel1
= $("#select-one select:first-child");
194 if ( typeof(sel1
[0].fireEvent
) !== "undefined" ) {
195 sel1
.trigger( "beforeactivate" ).clone().appendTo("#select-one");
196 //alert($("#select-one select").map(function(){ return this._change_attached || "undef"; }).get().join("|"));
199 jQuery
.fn
.blink = function(){
201 .css("backgroundColor","green")
202 .text( (parseInt(this.text(), 10) || 0) + 1 )
203 .delay(700).queue(function(next
){
204 jQuery(this).css("backgroundColor","#afa");
209 jQuery
.fn
.addSubmitTest = function( id
, prevent
) {
210 return this.live("submit", function(e
){
218 $("#text_submit").addSubmitTest("#textSubmit", true);
219 $("#password_submit").addSubmitTest("#passwordSubmit", true);
220 $("#submit_submit").addSubmitTest("#submitSubmit", true);
221 $("#prog_submit").addSubmitTest("#submitSubmit", true);
222 $(document
).bind("submit", function(){
223 jQuery("#boundSubmit").blink();