3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
6 test("attr(String)", function() {
9 // This one sometimes fails randomly ?!
10 equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
12 equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
13 equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
14 equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );
15 equals( jQuery('#check1').attr('type'), "checkbox", 'Check for type attribute' );
16 equals( jQuery('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );
17 equals( jQuery('#google').attr('title'), "Google!", 'Check for title attribute' );
18 equals( jQuery('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );
19 equals( jQuery('#en').attr('lang'), "en", 'Check for lang attribute' );
20 equals( jQuery('#simon').attr('class'), "blog link", 'Check for class attribute' );
21 equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );
22 equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );
23 ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
24 // Temporarily disabled. See: #4299
25 // ok( jQuery('#form').attr('action','newformaction').attr('action').indexOf("newformaction") >= 0, 'Check that action attribute was changed' );
26 equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
27 equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
28 equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
29 equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
30 equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
31 equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
33 // using innerHTML in IE causes href attribute to be serialized to the full path
34 jQuery('<a/>').attr({ 'id': 'tAnchor5', 'href': '#5' }).appendTo('#main');
35 equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
37 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
40 // Related to [5574] and [5683]
41 var body = document.body, $body = jQuery(body);
43 ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
44 ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
46 body.setAttribute('foo', 'baz');
47 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
50 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
52 $body.attr('foo','cool');
53 equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
56 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
58 body.removeAttribute('foo'); // Cleanup
60 var select = document.createElement("select"), optgroup = document.createElement("optgroup"), option = document.createElement("option");
61 optgroup.appendChild( option );
62 select.appendChild( optgroup );
64 equals( jQuery(option).attr("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
66 ok( jQuery("<div/>").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." );
67 ok( jQuery().attr("doesntexist") === undefined, "Make sure undefined is returned when no element is there." );
71 test("attr(String) in XML Files", function() {
74 jQuery.get("data/dashboard.xml", function(xml) {
75 equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
76 equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
82 test("attr(String, Function)", function() {
84 equals( jQuery('#text1').attr('value', function() { return this.id ;})[0].value, "text1", "Set value from id" );
85 equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
88 test("attr(Hash)", function() {
91 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
92 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
94 ok( pass, "Set Multiple Attributes" );
95 equals( jQuery('#text1').attr({'value': function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
96 equals( jQuery('#text1').attr({'title': function(i) { return i; }}).attr('title'), "0", "Set attribute to computed value #2");
100 test("attr(String, Object)", function() {
103 var div = jQuery("div").attr("foo", "bar"),
106 for ( var i = 0; i < div.size(); i++ ) {
107 if ( div.get(i).getAttribute('foo') != "bar" ){
113 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
115 // Fails on IE since recent changes to .attr()
116 // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
118 jQuery("#name").attr('name', 'something');
119 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
120 jQuery("#name").attr('name', null);
121 equals( jQuery("#name").attr('title'), '', 'Remove name attribute' );
122 jQuery("#check2").attr('checked', true);
123 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
124 jQuery("#check2").attr('checked', false);
125 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
126 jQuery("#text1").attr('readonly', true);
127 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
128 jQuery("#text1").attr('readonly', false);
129 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
130 jQuery("#name").attr('maxlength', '5');
131 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
132 jQuery("#name").attr('maxLength', '10');
133 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
135 var table = jQuery('#table').append("<tr><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr>"),
136 td = table.find('td:first');
137 td.attr("rowspan", "2");
138 equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
139 td.attr("colspan", "2");
140 equals( td[0].colSpan, 2, "Check colspan is correctly set" );
141 table.attr("cellspacing", "2");
142 equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
145 jQuery("#name").attr('someAttr', '0');
146 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
147 jQuery("#name").attr('someAttr', 0);
148 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
149 jQuery("#name").attr('someAttr', 1);
150 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
152 // using contents will get comments regular, text, and comment nodes
153 var j = jQuery("#nonnodes").contents();
155 j.attr("name", "attrvalue");
156 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
157 j.removeAttr("name");
161 var type = jQuery("#check2").attr('type');
164 jQuery("#check2").attr('type','hidden');
168 ok( thrown, "Exception thrown when trying to change type property" );
169 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
171 var check = document.createElement("input");
174 jQuery(check).attr('type','checkbox');
178 ok( thrown, "Exception thrown when trying to change type property" );
179 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
181 var check = jQuery("<input />");
184 check.attr('type','checkbox');
188 ok( thrown, "Exception thrown when trying to change type property" );
189 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
191 var button = jQuery("#button");
194 button.attr('type','submit');
198 ok( thrown, "Exception thrown when trying to change type property" );
199 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
202 test("attr(jquery_method)", function(){
205 var $elem = jQuery("<div />"),
209 $elem.attr({'html': 'foo'}, true);
210 equals( elem.innerHTML, 'foo', 'attr(html)');
212 $elem.attr({'text': 'bar'}, true);
213 equals( elem.innerHTML, 'bar', 'attr(text)');
215 $elem.attr({'css': {color:'red'}}, true);
216 ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
218 $elem.attr({'height': 10}, true);
219 equals( elem.style.height, '10px', 'attr(height)');
221 // Multiple attributes
225 css:{ paddingLeft:1, paddingRight:1 }
228 equals( elem.style.width, '10px', 'attr({...})');
229 equals( elem.style.paddingLeft, '1px', 'attr({...})');
230 equals( elem.style.paddingRight, '1px', 'attr({...})');
234 test("attr(String, Object) - Loaded via XML document", function() {
237 jQuery.get('data/dashboard.xml', function(xml) {
239 jQuery('tab', xml).each(function() {
240 titles.push(jQuery(this).attr('title'));
242 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
243 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
249 test("attr('tabindex')", function() {
252 // elements not natively tabbable
253 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
254 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
257 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
258 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
259 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
261 // anchor without href
262 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
263 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
264 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
267 test("attr('tabindex', value)", function() {
270 var element = jQuery('#divWithNoTabIndex');
271 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
273 // set a positive string
274 element.attr('tabindex', '1');
275 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
278 element.attr('tabindex', '0');
279 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
281 // set a negative string
282 element.attr('tabindex', '-1');
283 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
285 // set a positive number
286 element.attr('tabindex', 1);
287 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
290 element.attr('tabindex', 0);
291 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
293 // set a negative number
294 element.attr('tabindex', -1);
295 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
297 element = jQuery('#linkWithTabIndex');
298 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
300 element.attr('tabindex', -1);
301 equals(element.attr('tabindex'), -1, 'set negative tabindex');
304 test("removeAttr(String)", function() {
306 equals( jQuery('#mark').removeAttr( "class" )[0].className, "", "remove class" );
309 test("val()", function() {
312 document.getElementById('text1').value = "bla";
313 equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
317 equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
318 // ticket #1714 this caused a JS error in IE
319 equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
320 ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
322 equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
324 same( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
326 equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
328 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
330 equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
332 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with no value attribute' );
334 jQuery('#select3').val("");
335 same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );
337 same( jQuery('#select4').val(), [], 'Call val() on multiple="multiple" select with all disabled options' );
339 jQuery('#select4 optgroup').add('#select4 > [disabled]').attr('disabled', false);
340 same( jQuery('#select4').val(), ['2', '3'], 'Call val() on multiple="multiple" select with some disabled options' );
342 jQuery('#select4').attr('disabled', true);
343 same( jQuery('#select4').val(), ['2', '3'], 'Call val() on disabled multiple="multiple" select' );
345 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
347 jQuery('#select5').val(1);
348 equals( jQuery('#select5').val(), "1", "Check value on ambiguous select." );
350 jQuery('#select5').val(3);
351 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
353 var checks = jQuery("<input type='checkbox' name='test' value='1'/><input type='checkbox' name='test' value='2'/><input type='checkbox' name='test' value=''/><input type='checkbox' name='test'/>").appendTo("#form");
355 same( checks.serialize(), "", "Get unchecked values." );
357 equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
360 same( checks.serialize(), "test=2", "Get a single checked value." );
362 checks.val([ "1", "" ]);
363 same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
365 checks.val([ "", "2" ]);
366 same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
368 checks.val([ "1", "on" ]);
369 same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
374 var testVal = function(valueObj) {
377 jQuery("#text1").val(valueObj( 'test' ));
378 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
380 jQuery("#text1").val(valueObj( undefined ));
381 equals( document.getElementById('text1').value, "", "Check for modified (via val(undefined)) value of input element" );
383 jQuery("#text1").val(valueObj( 67 ));
384 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
386 jQuery("#text1").val(valueObj( null ));
387 equals( document.getElementById('text1').value, "", "Check for modified (via val(null)) value of input element" );
389 jQuery("#select1").val(valueObj( "3" ));
390 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
392 jQuery("#select1").val(valueObj( 2 ));
393 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
395 jQuery("#select1").append("<option value='4'>four</option>");
396 jQuery("#select1").val(valueObj( 4 ));
397 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
399 // using contents will get comments regular, text, and comment nodes
400 var j = jQuery("#nonnodes").contents();
401 j.val(valueObj( "asdf" ));
402 equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
403 j.removeAttr("value");
406 test("val(String/Number)", function() {
410 test("val(Function)", function() {
411 testVal(functionReturningObj);
414 test( "val(Array of Numbers) (Bug #7123)", function() {
416 jQuery('#form').append('<input type="checkbox" name="arrayTest" value="1" /><input type="checkbox" name="arrayTest" value="2" /><input type="checkbox" name="arrayTest" value="3" checked="checked" /><input type="checkbox" name="arrayTest" value="4" />');
417 var elements = jQuery('input[name=arrayTest]').val([ 1, 2 ]);
418 ok( elements[0].checked, "First element was checked" );
419 ok( elements[1].checked, "Second element was checked" );
420 ok( !elements[2].checked, "Third element was unchecked" );
421 ok( !elements[3].checked, "Fourth element remained unchecked" );
426 test("val(Function) with incoming value", function() {
429 var oldVal = jQuery("#text1").val();
431 jQuery("#text1").val(function(i, val) {
432 equals( val, oldVal, "Make sure the incoming value is correct." );
436 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
438 oldVal = jQuery("#text1").val();
440 jQuery("#text1").val(function(i, val) {
441 equals( val, oldVal, "Make sure the incoming value is correct." );
445 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
447 oldVal = jQuery("#select1").val();
449 jQuery("#select1").val(function(i, val) {
450 equals( val, oldVal, "Make sure the incoming value is correct." );
454 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
456 oldVal = jQuery("#select1").val();
458 jQuery("#select1").val(function(i, val) {
459 equals( val, oldVal, "Make sure the incoming value is correct." );
463 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
465 jQuery("#select1").append("<option value='4'>four</option>");
467 oldVal = jQuery("#select1").val();
469 jQuery("#select1").val(function(i, val) {
470 equals( val, oldVal, "Make sure the incoming value is correct." );
474 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
477 var testAddClass = function(valueObj) {
479 var div = jQuery("div");
480 div.addClass( valueObj("test") );
482 for ( var i = 0; i < div.size(); i++ ) {
483 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
485 ok( pass, "Add Class" );
487 // using contents will get regular, text, and comment nodes
488 var j = jQuery("#nonnodes").contents();
489 j.addClass( valueObj("asdf") );
490 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
492 div = jQuery("<div/>");
494 div.addClass( valueObj("test") );
495 equals( div.attr("class"), "test", "Make sure there's no extra whitespace." );
497 div.attr("class", " foo");
498 div.addClass( valueObj("test") );
499 equals( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
501 div.attr("class", "foo");
502 div.addClass( valueObj("bar baz") );
503 equals( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
506 test("addClass(String)", function() {
507 testAddClass(bareObj);
510 test("addClass(Function)", function() {
511 testAddClass(functionReturningObj);
514 test("addClass(Function) with incoming value", function() {
517 var div = jQuery("div"), old = div.map(function(){
518 return jQuery(this).attr("class");
521 div.addClass(function(i, val) {
522 if ( this.id !== "_firebugConsole" ) {
523 equals( val, old[i], "Make sure the incoming value is correct." );
529 for ( var i = 0; i < div.size(); i++ ) {
530 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
532 ok( pass, "Add Class" );
535 var testRemoveClass = function(valueObj) {
538 var $divs = jQuery('div');
540 $divs.addClass("test").removeClass( valueObj("test") );
542 ok( !$divs.is('.test'), "Remove Class" );
545 $divs = jQuery('div');
547 $divs.addClass("test").addClass("foo").addClass("bar");
548 $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
550 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
553 $divs = jQuery('div');
555 // Make sure that a null value doesn't cause problems
556 $divs.eq(0).addClass("test").removeClass( valueObj(null) );
557 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
559 $divs.eq(0).addClass("test").removeClass( valueObj("") );
560 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
562 // using contents will get regular, text, and comment nodes
563 var j = jQuery("#nonnodes").contents();
564 j.removeClass( valueObj("asdf") );
565 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
567 var div = document.createElement("div");
568 div.className = " test foo ";
570 jQuery(div).removeClass( valueObj("foo") );
571 equals( div.className, "test", "Make sure remaining className is trimmed." );
573 div.className = " test ";
575 jQuery(div).removeClass( valueObj("test") );
576 equals( div.className, "", "Make sure there is nothing left after everything is removed." );
579 test("removeClass(String) - simple", function() {
580 testRemoveClass(bareObj);
583 test("removeClass(Function) - simple", function() {
584 testRemoveClass(functionReturningObj);
587 test("removeClass(Function) with incoming value", function() {
590 var $divs = jQuery('div').addClass("test"), old = $divs.map(function(){
591 return jQuery(this).attr("class");
594 $divs.removeClass(function(i, val) {
595 if ( this.id !== "_firebugConsole" ) {
596 equals( val, old[i], "Make sure the incoming value is correct." );
601 ok( !$divs.is('.test'), "Remove Class" );
606 var testToggleClass = function(valueObj) {
609 var e = jQuery("#firstp");
610 ok( !e.is(".test"), "Assert class not present" );
611 e.toggleClass( valueObj("test") );
612 ok( e.is(".test"), "Assert class present" );
613 e.toggleClass( valueObj("test") );
614 ok( !e.is(".test"), "Assert class not present" );
616 // class name with a boolean
617 e.toggleClass( valueObj("test"), false );
618 ok( !e.is(".test"), "Assert class not present" );
619 e.toggleClass( valueObj("test"), true );
620 ok( e.is(".test"), "Assert class present" );
621 e.toggleClass( valueObj("test"), false );
622 ok( !e.is(".test"), "Assert class not present" );
624 // multiple class names
625 e.addClass("testA testB");
626 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
627 e.toggleClass( valueObj("testB testC") );
628 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
629 e.toggleClass( valueObj("testA testC") );
630 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
632 // toggleClass storage
634 ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
635 e.addClass("testD testE");
636 ok( e.is(".testD.testE"), "Assert class present" );
638 ok( !e.is(".testD.testE"), "Assert class not present" );
639 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
641 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
642 e.toggleClass(false);
643 ok( !e.is(".testD.testE"), "Assert class not present" );
645 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
647 e.toggleClass(false);
649 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
654 e.removeClass("testD");
655 e.removeData('__className__');
658 test("toggleClass(String|boolean|undefined[, boolean])", function() {
659 testToggleClass(bareObj);
662 test("toggleClass(Function[, boolean])", function() {
663 testToggleClass(functionReturningObj);
666 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
669 var e = jQuery("#firstp"), old = e.attr("class");
670 ok( !e.is(".test"), "Assert class not present" );
672 e.toggleClass(function(i, val) {
673 equals( val, old, "Make sure the incoming value is correct." );
676 ok( e.is(".test"), "Assert class present" );
678 old = e.attr("class");
680 e.toggleClass(function(i, val) {
681 equals( val, old, "Make sure the incoming value is correct." );
684 ok( !e.is(".test"), "Assert class not present" );
686 old = e.attr("class");
688 // class name with a boolean
689 e.toggleClass(function(i, val, state) {
690 equals( val, old, "Make sure the incoming value is correct." );
691 equals( state, false, "Make sure that the state is passed in." );
694 ok( !e.is(".test"), "Assert class not present" );
696 old = e.attr("class");
698 e.toggleClass(function(i, val, state) {
699 equals( val, old, "Make sure the incoming value is correct." );
700 equals( state, true, "Make sure that the state is passed in." );
703 ok( e.is(".test"), "Assert class present" );
705 old = e.attr("class");
707 e.toggleClass(function(i, val, state) {
708 equals( val, old, "Make sure the incoming value is correct." );
709 equals( state, false, "Make sure that the state is passed in." );
712 ok( !e.is(".test"), "Assert class not present" );
715 e.removeClass("test");
716 e.removeData('__className__');
719 test("addClass, removeClass, hasClass", function() {
722 var jq = jQuery("<p>Hi</p>"), x = jq[0];
725 equals( x.className, "hi", "Check single added class" );
727 jq.addClass("foo bar");
728 equals( x.className, "hi foo bar", "Check more added classes" );
731 equals( x.className, "", "Remove all classes" );
733 jq.addClass("hi foo bar");
734 jq.removeClass("foo");
735 equals( x.className, "hi bar", "Check removal of one class" );
737 ok( jq.hasClass("hi"), "Check has1" );
738 ok( jq.hasClass("bar"), "Check has2" );
740 var jq = jQuery("<p class='class1\nclass2\tcla.ss3\n'></p>");
741 ok( jq.hasClass("class1"), "Check hasClass with carriage return" );
742 ok( jq.is(".class1"), "Check is with carriage return" );
743 ok( jq.hasClass("class2"), "Check hasClass with tab" );
744 ok( jq.is(".class2"), "Check is with tab" );
745 ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
747 jq.removeClass("class2");
748 ok( jq.hasClass("class2")==false, "Check the class has been properly removed" );
749 jq.removeClass("cla");
750 ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
751 jq.removeClass("cla.ss3");
752 ok( jq.hasClass("cla.ss3")==false, "Check the dotted class has been removed" );