1 module( "attributes", {
2 teardown: moduleTeardown
5 function bareObj( value ) {
9 function functionReturningObj( value ) {
16 ======== local reference =======
17 bareObj and functionReturningObj can be used to test passing functions to setters
18 See testVal below for an example
21 This function returns whatever value is passed in
23 functionReturningObj( value );
24 Returns a function that returns the value
27 test( "jQuery.propFix integrity test", function() {
30 // This must be maintained and equal jQuery.attrFix when appropriate
31 // Ensure that accidental or erroneous property
32 // overwrites don't occur
33 // This is simply for better code coverage and future proofing.
35 "tabindex": "tabIndex",
36 "readonly": "readOnly",
39 "maxlength": "maxLength",
40 "cellspacing": "cellSpacing",
41 "cellpadding": "cellPadding",
45 "frameborder": "frameBorder",
46 "contenteditable": "contentEditable"
49 deepEqual( props, jQuery.propFix, "jQuery.propFix passes integrity check" );
52 test( "attr(String)", function() {
55 var extras, body, $body,
56 select, optgroup, option, $img, styleElem,
59 equal( jQuery("#text1").attr("type"), "text", "Check for type attribute" );
60 equal( jQuery("#radio1").attr("type"), "radio", "Check for type attribute" );
61 equal( jQuery("#check1").attr("type"), "checkbox", "Check for type attribute" );
62 equal( jQuery("#simon1").attr("rel"), "bookmark", "Check for rel attribute" );
63 equal( jQuery("#google").attr("title"), "Google!", "Check for title attribute" );
64 equal( jQuery("#mark").attr("hreflang"), "en", "Check for hreflang attribute" );
65 equal( jQuery("#en").attr("lang"), "en", "Check for lang attribute" );
66 equal( jQuery("#simon").attr("class"), "blog link", "Check for class attribute" );
67 equal( jQuery("#name").attr("name"), "name", "Check for name attribute" );
68 equal( jQuery("#text1").attr("name"), "action", "Check for name attribute" );
69 ok( jQuery("#form").attr("action").indexOf("formaction") >= 0, "Check for action attribute" );
70 equal( jQuery("#text1").attr("value", "t").attr("value"), "t", "Check setting the value attribute" );
71 equal( jQuery("#text1").attr("value", "").attr("value"), "", "Check setting the value attribute to empty string" );
72 equal( jQuery("<div value='t'></div>").attr("value"), "t", "Check setting custom attr named 'value' on a div" );
73 equal( jQuery("#form").attr("blah", "blah").attr("blah"), "blah", "Set non-existent attribute on a form" );
74 equal( jQuery("#foo").attr("height"), undefined, "Non existent height attribute should return undefined" );
76 // [7472] & [3113] (form contains an input with name="action" or name="id")
77 extras = jQuery("<input id='id' name='id' /><input id='name' name='name' /><input id='target' name='target' />").appendTo("#testForm");
78 equal( jQuery("#form").attr("action","newformaction").attr("action"), "newformaction", "Check that action attribute was changed" );
79 equal( jQuery("#testForm").attr("target"), undefined, "Retrieving target does not equal the input with name=target" );
80 equal( jQuery("#testForm").attr("target", "newTarget").attr("target"), "newTarget", "Set target successfully on a form" );
81 equal( jQuery("#testForm").removeAttr("id").attr("id"), undefined, "Retrieving id does not equal the input with name=id after id is removed [#7472]" );
82 // Bug #3685 (form contains input with name="name")
83 equal( jQuery("#testForm").attr("name"), undefined, "Retrieving name does not retrieve input with name=name" );
86 equal( jQuery("#text1").attr("maxlength"), "30", "Check for maxlength attribute" );
87 equal( jQuery("#text1").attr("maxLength"), "30", "Check for maxLength attribute" );
88 equal( jQuery("#area1").attr("maxLength"), "30", "Check for maxLength attribute" );
90 // using innerHTML in IE causes href attribute to be serialized to the full path
94 }).appendTo("#qunit-fixture");
95 equal( jQuery("#tAnchor5").attr("href"), "#5", "Check for non-absolute href (an anchor)" );
96 jQuery("<a id='tAnchor6' href='#5' />").appendTo("#qunit-fixture");
97 equal( jQuery("#tAnchor5").prop("href"), jQuery("#tAnchor6").prop("href"), "Check for absolute href prop on an anchor" );
99 jQuery("<script type='jquery/test' src='#5' id='scriptSrc'></script>").appendTo("#qunit-fixture");
100 equal( jQuery("#tAnchor5").prop("href"), jQuery("#scriptSrc").prop("src"), "Check for absolute src prop on a script" );
102 // list attribute is readonly by default in browsers that support it
103 jQuery("#list-test").attr( "list", "datalist" );
104 equal( jQuery("#list-test").attr("list"), "datalist", "Check setting list attribute" );
106 // Related to [5574] and [5683]
107 body = document.body;
108 $body = jQuery( body );
110 strictEqual( $body.attr("foo"), undefined, "Make sure that a non existent attribute returns undefined" );
112 body.setAttribute( "foo", "baz" );
113 equal( $body.attr("foo"), "baz", "Make sure the dom attribute is retrieved when no expando is found" );
115 $body.attr( "foo","cool" );
116 equal( $body.attr("foo"), "cool", "Make sure that setting works well when both expando and dom attribute are available" );
118 body.removeAttribute("foo"); // Cleanup
120 select = document.createElement("select");
121 optgroup = document.createElement("optgroup");
122 option = document.createElement("option");
124 optgroup.appendChild( option );
125 select.appendChild( optgroup );
127 equal( jQuery( option ).prop("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
129 $img = jQuery("<img style='display:none' width='215' height='53' src='data/1x1.jpg'/>").appendTo("body");
130 equal( $img.attr("width"), "215", "Retrieve width attribute an an element with display:none." );
131 equal( $img.attr("height"), "53", "Retrieve height attribute an an element with display:none." );
133 // Check for style support
134 styleElem = jQuery("<div/>").appendTo("#qunit-fixture").css({
135 background: "url(UPPERlower.gif)"
137 ok( !!~styleElem.attr("style").indexOf("UPPERlower.gif"), "Check style attribute getter" );
138 ok( !!~styleElem.attr("style", "position:absolute;").attr("style").indexOf("absolute"), "Check style setter" );
140 // Check value on button element (#1954)
141 $button = jQuery("<button>text</button>").insertAfter("#button");
142 strictEqual( $button.attr("value"), undefined, "Absence of value attribute on a button" );
143 equal( $button.attr( "value", "foobar" ).attr("value"), "foobar", "Value attribute on a button does not return innerHTML" );
144 equal( $button.attr("value", "baz").html(), "text", "Setting the value attribute does not change innerHTML" );
146 // Attributes with a colon on a table element (#1591)
147 equal( jQuery("#table").attr("test:attrib"), undefined, "Retrieving a non-existent attribute on a table with a colon does not throw an error." );
148 equal( jQuery("#table").attr( "test:attrib", "foobar" ).attr("test:attrib"), "foobar", "Setting an attribute on a table with a colon does not throw an error." );
150 $form = jQuery("<form class='something'></form>").appendTo("#qunit-fixture");
151 equal( $form.attr("class"), "something", "Retrieve the class attribute on a form." );
153 $a = jQuery("<a href='#' onclick='something()'>Click</a>").appendTo("#qunit-fixture");
154 equal( $a.attr("onclick"), "something()", "Retrieve ^on attribute without anonymous function wrapper." );
156 ok( jQuery("<div/>").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." );
157 ok( jQuery("<div/>").attr("title") === undefined, "Make sure undefined is returned when no attribute is found." );
158 equal( jQuery("<div/>").attr( "title", "something" ).attr("title"), "something", "Set the title attribute." );
159 ok( jQuery().attr("doesntexist") === undefined, "Make sure undefined is returned when no element is there." );
160 equal( jQuery("<div/>").attr("value"), undefined, "An unset value on a div returns undefined." );
161 strictEqual( jQuery("<select><option value='property'></option></select>").attr("value"), undefined, "An unset value on a select returns undefined." );
163 $form = jQuery("#form").attr( "enctype", "multipart/form-data" );
164 equal( $form.prop("enctype"), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 #6743)" );
168 test( "attr(String) on cloned elements, #9646", function() {
172 input = jQuery("<input name='tester' />");
176 strictEqual( input.clone( true ).attr( "name", "test" )[ 0 ].name, "test", "Name attribute should be changed on cloned element" );
178 div = jQuery("<div id='tester' />");
181 strictEqual( div.clone( true ).attr( "id", "test" )[ 0 ].id, "test", "Id attribute should be changed on cloned element" );
183 input = jQuery("<input value='tester' />");
186 strictEqual( input.clone( true ).attr( "value", "test" )[ 0 ].value, "test", "Value attribute should be changed on cloned element" );
188 strictEqual( input.clone( true ).attr( "value", 42 )[ 0 ].value, "42", "Value attribute should be changed on cloned element" );
191 test( "attr(String) in XML Files", function() {
193 var xml = createDashboardXML();
194 equal( jQuery( "locations", xml ).attr("class"), "foo", "Check class attribute in XML document" );
195 equal( jQuery( "location", xml ).attr("for"), "bar", "Check for attribute in XML document" );
196 equal( jQuery( "location", xml ).attr("checked"), "different", "Check that hooks are not attached in XML document" );
199 test( "attr(String, Function)", function() {
203 jQuery("#text1").attr( "value", function() {
211 jQuery("#text1").attr( "title", function(i) {
215 "Set value with an index"
219 test( "attr(Hash)", function() {
226 if ( this.getAttribute("foo") !== "baz" && this.getAttribute("zoo") !== "ping" ) {
231 ok( pass, "Set Multiple Attributes" );
234 jQuery("#text1").attr({
235 "value": function() {
239 "Set attribute to computed value #1"
243 jQuery("#text1").attr({
244 "title": function(i) {
249 "Set attribute to computed value #2"
253 test( "attr(String, Object)", function() {
256 var $input, $text, $details,
257 attributeNode, commentNode, textNode, obj,
259 check, thrown, button, $radio, $radios, $svg,
260 div = jQuery("div").attr("foo", "bar"),
264 for ( ; i < div.length; i++ ) {
265 if ( div[ i ].getAttribute("foo") !== "bar" ) {
271 equal( fail, false, "Set Attribute, the #" + fail + " element didn't get the attribute 'foo'" );
274 jQuery("#foo").attr({
277 "Try to set an attribute to nothing"
280 jQuery("#name").attr( "name", "something" );
281 equal( jQuery("#name").attr("name"), "something", "Set name attribute" );
282 jQuery("#name").attr( "name", null );
283 equal( jQuery("#name").attr("name"), undefined, "Remove name attribute" );
285 $input = jQuery( "<input>", {
289 equal( $input.attr("name"), "something", "Check element creation gets/sets the name attribute." );
290 equal( $input.attr("id"), "specified", "Check element creation gets/sets the id attribute." );
292 // As of fixing #11115, we only guarantee boolean property update for checked and selected
293 $input = jQuery("<input type='checkbox'/>").attr( "checked", true );
294 equal( $input.prop("checked"), true, "Setting checked updates property (verified by .prop)" );
295 equal( $input[0].checked, true, "Setting checked updates property (verified by native property)" );
296 $input = jQuery("<option/>").attr( "selected", true );
297 equal( $input.prop("selected"), true, "Setting selected updates property (verified by .prop)" );
298 equal( $input[0].selected, true, "Setting selected updates property (verified by native property)" );
300 $input = jQuery("#check2");
301 $input.prop( "checked", true ).prop( "checked", false ).attr( "checked", true );
302 equal( $input.attr("checked"), "checked", "Set checked (verified by .attr)" );
303 $input.prop( "checked", false ).prop( "checked", true ).attr( "checked", false );
304 equal( $input.attr("checked"), undefined, "Remove checked (verified by .attr)" );
306 $input = jQuery("#text1").prop( "readOnly", true ).prop( "readOnly", false ).attr( "readonly", true );
307 equal( $input.attr("readonly"), "readonly", "Set readonly (verified by .attr)" );
308 $input.prop( "readOnly", false ).prop( "readOnly", true ).attr( "readonly", false );
309 equal( $input.attr("readonly"), undefined, "Remove readonly (verified by .attr)" );
311 $input = jQuery("#check2").attr( "checked", true ).attr( "checked", false ).prop( "checked", true );
312 equal( $input[0].checked, true, "Set checked property (verified by native property)" );
313 equal( $input.prop("checked"), true, "Set checked property (verified by .prop)" );
314 equal( $input.attr("checked"), undefined, "Setting checked property doesn't affect checked attribute" );
315 $input.attr( "checked", false ).attr( "checked", true ).prop( "checked", false );
316 equal( $input[0].checked, false, "Clear checked property (verified by native property)" );
317 equal( $input.prop("checked"), false, "Clear checked property (verified by .prop)" );
318 equal( $input.attr("checked"), "checked", "Clearing checked property doesn't affect checked attribute" );
320 $input = jQuery("#check2").attr( "checked", false ).attr( "checked", "checked" );
321 equal( $input.attr("checked"), "checked", "Set checked to 'checked' (verified by .attr)" );
323 $radios = jQuery("#checkedtest").find("input[type='radio']");
324 $radios.eq( 1 ).trigger("click");
325 equal( $radios.eq( 1 ).prop("checked"), true, "Second radio was checked when clicked" );
326 equal( $radios.eq( 0 ).attr("checked"), "checked", "First radio is still [checked]" );
328 $input = jQuery("#text1").attr( "readonly", false ).prop( "readOnly", true );
329 equal( $input[0].readOnly, true, "Set readonly property (verified by native property)" );
330 equal( $input.prop("readOnly"), true, "Set readonly property (verified by .prop)" );
331 $input.attr( "readonly", true ).prop( "readOnly", false );
332 equal( $input[0].readOnly, false, "Clear readonly property (verified by native property)" );
333 equal( $input.prop("readOnly"), false, "Clear readonly property (verified by .prop)" );
335 $input = jQuery("#name").attr( "maxlength", "5" );
336 equal( $input[0].maxLength, 5, "Set maxlength (verified by native property)" );
337 $input.attr( "maxLength", "10" );
338 equal( $input[0].maxLength, 10, "Set maxlength (verified by native property)" );
340 // HTML5 boolean attributes
341 $text = jQuery("#text1").attr({
345 equal( $text.attr("autofocus"), "autofocus", "Reading autofocus attribute yields 'autofocus'" );
346 equal( $text.attr( "autofocus", false ).attr("autofocus"), undefined, "Setting autofocus to false removes it" );
347 equal( $text.attr("required"), "required", "Reading required attribute yields 'required'" );
348 equal( $text.attr( "required", false ).attr("required"), undefined, "Setting required attribute to false removes it" );
350 $details = jQuery("<details open></details>").appendTo("#qunit-fixture");
351 equal( $details.attr("open"), "open", "open attribute presence indicates true" );
352 equal( $details.attr( "open", false ).attr("open"), undefined, "Setting open attribute to false removes it" );
354 $text.attr( "data-something", true );
355 equal( $text.attr("data-something"), "true", "Set data attributes");
356 equal( $text.data("something"), true, "Setting data attributes are not affected by boolean settings");
357 $text.attr( "data-another", false );
358 equal( $text.attr("data-another"), "false", "Set data attributes");
359 equal( $text.data("another"), false, "Setting data attributes are not affected by boolean settings" );
360 equal( $text.attr( "aria-disabled", false ).attr("aria-disabled"), "false", "Setting aria attributes are not affected by boolean settings" );
361 $text.removeData("something").removeData("another").removeAttr("aria-disabled");
363 jQuery("#foo").attr("contenteditable", true);
364 equal( jQuery("#foo").attr("contenteditable"), "true", "Enumerated attributes are set properly" );
366 attributeNode = document.createAttribute("irrelevant");
367 commentNode = document.createComment("some comment");
368 textNode = document.createTextNode("some text");
371 jQuery.each( [ commentNode, textNode, attributeNode ], function( i, elem ) {
372 var $elem = jQuery( elem );
373 $elem.attr( "nonexisting", "foo" );
374 strictEqual( $elem.attr("nonexisting"), undefined, "attr(name, value) works correctly on comment and text nodes (bug #7500)." );
377 jQuery.each( [ window, document, obj, "#firstp" ], function( i, elem ) {
378 var oldVal = elem.nonexisting,
379 $elem = jQuery( elem );
380 strictEqual( $elem.attr("nonexisting"), undefined, "attr works correctly for non existing attributes (bug #7500)." );
381 equal( $elem.attr( "nonexisting", "foo" ).attr("nonexisting"), "foo", "attr falls back to prop on unsupported arguments" );
382 elem.nonexisting = oldVal;
385 // Register the property on the window for the previous assertion so it will be clean up
386 Globals.register( "nonexisting" );
388 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>");
389 td = table.find("td").eq(0);
390 td.attr( "rowspan", "2" );
391 equal( td[ 0 ]["rowSpan"], 2, "Check rowspan is correctly set" );
392 td.attr( "colspan", "2" );
393 equal( td[ 0 ]["colSpan"], 2, "Check colspan is correctly set" );
394 table.attr("cellspacing", "2");
395 equal( table[ 0 ]["cellSpacing"], "2", "Check cellspacing is correctly set" );
397 equal( jQuery("#area1").attr("value"), undefined, "Value attribute is distinct from value property." );
400 jQuery("#name").attr( "someAttr", "0" );
401 equal( jQuery("#name").attr("someAttr"), "0", "Set attribute to a string of '0'" );
402 jQuery("#name").attr( "someAttr", 0 );
403 equal( jQuery("#name").attr("someAttr"), "0", "Set attribute to the number 0" );
404 jQuery("#name").attr( "someAttr", 1 );
405 equal( jQuery("#name").attr("someAttr"), "1", "Set attribute to the number 1" );
407 // using contents will get comments regular, text, and comment nodes
408 j = jQuery("#nonnodes").contents();
410 j.attr( "name", "attrvalue" );
411 equal( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
412 j.removeAttr("name");
415 type = jQuery("#check2").attr("type");
417 jQuery("#check2").attr( "type", "hidden" );
418 ok( true, "No exception thrown on input type change" );
420 ok( true, "Exception thrown on input type change: " + e );
423 check = document.createElement("input");
426 jQuery( check ).attr( "type", "checkbox" );
430 ok( thrown, "Exception thrown when trying to change type property" );
431 equal( "checkbox", jQuery( check ).attr("type"), "Verify that you can change the type of an input element that isn't in the DOM" );
433 check = jQuery("<input />");
436 check.attr( "type", "checkbox" );
440 ok( thrown, "Exception thrown when trying to change type property" );
441 equal( "checkbox", check.attr("type"), "Verify that you can change the type of an input element that isn't in the DOM" );
443 button = jQuery("#button");
445 button.attr( "type", "submit" );
446 ok( true, "No exception thrown on button type change" );
448 ok( true, "Exception thrown on button type change: " + e );
451 $radio = jQuery( "<input>", {
454 }).appendTo("#testForm");
455 equal( $radio.val(), "sup", "Value is not reset when type is set after value on a radio" );
457 // Setting attributes on svg elements (bug #3116)
459 "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' version='1.1' baseProfile='full' width='200' height='200'>" +
461 "<circle cx='200' cy='200' r='150' />" +
464 equal( $svg.attr( "cx", 100 ).attr("cx"), "100", "Set attribute on svg element" );
467 // undefined values are chainable
468 jQuery("#name").attr( "maxlength", "5" ).removeAttr("nonexisting");
469 equal( typeof jQuery("#name").attr( "maxlength", undefined ), "object", ".attr('attribute', undefined) is chainable (#5571)" );
470 equal( jQuery("#name").attr( "maxlength", undefined ).attr("maxlength"), "5", ".attr('attribute', undefined) does not change value (#5571)" );
471 equal( jQuery("#name").attr( "nonexisting", undefined ).attr("nonexisting"), undefined, ".attr('attribute', undefined) does not create attribute (#5571)" );
474 test( "attr - extending the boolean attrHandle", function() {
477 _handle = jQuery.expr.attrHandle.checked || $.noop;
478 jQuery.expr.attrHandle.checked = function() {
480 _handle.apply( this, arguments );
482 jQuery( "input" ).attr( "checked" );
484 jQuery( "input" ).attr( "checked" );
485 ok( called, "The boolean attrHandle does not drop custom attrHandles" );
488 test( "attr(String, Object) - Loaded via XML document", function() {
490 var xml = createDashboardXML(),
492 jQuery( "tab", xml ).each(function() {
493 titles.push( jQuery( this ).attr("title") );
495 equal( titles[ 0 ], "Location", "attr() in XML context: Check first title" );
496 equal( titles[ 1 ], "Users", "attr() in XML context: Check second title" );
499 test( "attr(String, Object) - Loaded via XML fragment", function() {
501 var frag = createXMLFragment(),
502 $frag = jQuery( frag );
504 $frag.attr( "test", "some value" );
505 equal( $frag.attr("test"), "some value", "set attribute" );
506 $frag.attr( "test", null );
507 equal( $frag.attr("test"), undefined, "remove attribute" );
510 test( "attr('tabindex')", function() {
513 // elements not natively tabbable
514 equal( jQuery("#listWithTabIndex").attr("tabindex"), "5", "not natively tabbable, with tabindex set to 0" );
515 equal( jQuery("#divWithNoTabIndex").attr("tabindex"), undefined, "not natively tabbable, no tabindex set" );
518 equal( jQuery("#linkWithNoTabIndex").attr("tabindex"), undefined, "anchor with href, no tabindex set" );
519 equal( jQuery("#linkWithTabIndex").attr("tabindex"), "2", "anchor with href, tabindex set to 2" );
520 equal( jQuery("#linkWithNegativeTabIndex").attr("tabindex"), "-1", "anchor with href, tabindex set to -1" );
522 // anchor without href
523 equal( jQuery("#linkWithNoHrefWithNoTabIndex").attr("tabindex"), undefined, "anchor without href, no tabindex set" );
524 equal( jQuery("#linkWithNoHrefWithTabIndex").attr("tabindex"), "1", "anchor without href, tabindex set to 2" );
525 equal( jQuery("#linkWithNoHrefWithNegativeTabIndex").attr("tabindex"), "-1", "anchor without href, no tabindex set" );
528 test( "attr('tabindex', value)", function() {
531 var element = jQuery("#divWithNoTabIndex");
532 equal( element.attr("tabindex"), undefined, "start with no tabindex" );
534 // set a positive string
535 element.attr( "tabindex", "1" );
536 equal( element.attr("tabindex"), "1", "set tabindex to 1 (string)" );
539 element.attr( "tabindex", "0" );
540 equal( element.attr("tabindex"), "0", "set tabindex to 0 (string)" );
542 // set a negative string
543 element.attr( "tabindex", "-1" );
544 equal( element.attr("tabindex"), "-1", "set tabindex to -1 (string)" );
546 // set a positive number
547 element.attr( "tabindex", 1 );
548 equal( element.attr("tabindex"), "1", "set tabindex to 1 (number)" );
551 element.attr( "tabindex", 0 );
552 equal(element.attr("tabindex"), "0", "set tabindex to 0 (number)");
554 // set a negative number
555 element.attr( "tabindex", -1 );
556 equal( element.attr("tabindex"), "-1", "set tabindex to -1 (number)" );
558 element = jQuery("#linkWithTabIndex");
559 equal( element.attr("tabindex"), "2", "start with tabindex 2" );
561 element.attr( "tabindex", -1 );
562 equal( element.attr("tabindex"), "-1", "set negative tabindex" );
565 test( "removeAttr(String)", function() {
569 equal( jQuery("#mark").removeAttr("class").attr("class"), undefined, "remove class" );
570 equal( jQuery("#form").removeAttr("id").attr("id"), undefined, "Remove id" );
571 equal( jQuery("#foo").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), undefined, "Check removing style attribute" );
572 equal( jQuery("#form").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), undefined, "Check removing style attribute on a form" );
573 equal( jQuery("<div style='position: absolute'></div>").appendTo("#foo").removeAttr("style").prop("style").cssText, "", "Check removing style attribute (#9699 Webkit)" );
574 equal( jQuery("#fx-test-group").attr( "height", "3px" ).removeAttr("height").get( 0 ).style.height, "1px", "Removing height attribute has no effect on height set with style attribute" );
576 jQuery("#check1").removeAttr("checked").prop( "checked", true ).removeAttr("checked");
577 equal( document.getElementById("check1").checked, false, "removeAttr sets boolean properties to false" );
578 jQuery("#text1").prop( "readOnly", true ).removeAttr("readonly");
579 equal( document.getElementById("text1").readOnly, false, "removeAttr sets boolean properties to false" );
581 jQuery("#option2c").removeAttr("selected");
582 equal( jQuery("#option2d").attr("selected"), "selected", "Removing `selected` from an option that is not selected does not remove selected from the currently selected option (#10870)" );
585 $first = jQuery("#first").attr( "contenteditable", "true" ).removeAttr("contenteditable");
586 equal( $first.attr("contenteditable"), undefined, "Remove the contenteditable attribute" );
588 ok( false, "Removing contenteditable threw an error (#10429)" );
591 $first = jQuery("<div Case='mixed'></div>");
592 equal( $first.attr("Case"), "mixed", "case of attribute doesn't matter" );
593 $first.removeAttr("Case");
594 equal( $first.attr( "Case" ), undefined, "mixed-case attribute was removed" );
597 test( "removeAttr(String) in XML", function() {
599 var xml = createDashboardXML(),
600 iwt = jQuery( "infowindowtab", xml );
602 equal( iwt.attr("normal"), "ab", "Check initial value" );
603 iwt.removeAttr("Normal");
604 equal( iwt.attr("normal"), "ab", "Should still be there" );
605 iwt.removeAttr("normal");
606 equal( iwt.attr("normal"), undefined, "Removed" );
608 equal( iwt.attr("mixedCase"), "yes", "Check initial value" );
609 equal( iwt.attr("mixedcase"), undefined, "toLowerCase not work good" );
610 iwt.removeAttr("mixedcase");
611 equal( iwt.attr("mixedCase"), "yes", "Should still be there" );
612 iwt.removeAttr("mixedCase");
613 equal( iwt.attr("mixedCase"), undefined, "Removed" );
616 test( "removeAttr(Multi String, variable space width)", function() {
619 var div = jQuery("<div id='a' alt='b' title='c' rel='d'></div>"),
627 jQuery.each( tests, function( key, val ) {
628 equal( div.attr( key ), val, "Attribute `" + key + "` exists, and has a value of `" + val + "`" );
631 div.removeAttr( "id alt title rel " );
633 jQuery.each( tests, function( key ) {
634 equal( div.attr( key ), undefined, "Attribute `" + key + "` was removed" );
638 test( "prop(String, Object)", function() {
642 equal( jQuery("#text1").prop("value"), "Test", "Check for value attribute" );
643 equal( jQuery("#text1").prop( "value", "Test2" ).prop("defaultValue"), "Test", "Check for defaultValue attribute" );
644 equal( jQuery("#select2").prop("selectedIndex"), 3, "Check for selectedIndex attribute" );
645 equal( jQuery("#foo").prop("nodeName").toUpperCase(), "DIV", "Check for nodeName attribute" );
646 equal( jQuery("#foo").prop("tagName").toUpperCase(), "DIV", "Check for tagName attribute" );
647 equal( jQuery("<option/>").prop("selected"), false, "Check selected attribute on disconnected element." );
649 equal( jQuery("#listWithTabIndex").prop("tabindex"), 5, "Check retrieving tabindex" );
650 jQuery("#text1").prop( "readonly", true );
651 equal( document.getElementById("text1").readOnly, true, "Check setting readOnly property with 'readonly'" );
652 equal( jQuery("#label-for").prop("for"), "action", "Check retrieving htmlFor" );
653 jQuery("#text1").prop("class", "test");
654 equal( document.getElementById("text1").className, "test", "Check setting className with 'class'" );
655 equal( jQuery("#text1").prop("maxlength"), 30, "Check retrieving maxLength" );
656 jQuery("#table").prop( "cellspacing", 1 );
657 equal( jQuery("#table").prop("cellSpacing"), "1", "Check setting and retrieving cellSpacing" );
658 jQuery("#table").prop( "cellpadding", 1 );
659 equal( jQuery("#table").prop("cellPadding"), "1", "Check setting and retrieving cellPadding" );
660 jQuery("#table").prop( "rowspan", 1 );
661 equal( jQuery("#table").prop("rowSpan"), 1, "Check setting and retrieving rowSpan" );
662 jQuery("#table").prop( "colspan", 1 );
663 equal( jQuery("#table").prop("colSpan"), 1, "Check setting and retrieving colSpan" );
664 jQuery("#table").prop( "usemap", 1 );
665 equal( jQuery("#table").prop("useMap"), 1, "Check setting and retrieving useMap" );
666 jQuery("#table").prop( "frameborder", 1 );
667 equal( jQuery("#table").prop("frameBorder"), 1, "Check setting and retrieving frameBorder" );
670 test( "prop(String, Object) on null/undefined", function() {
674 var select, optgroup, option, attributeNode, commentNode, textNode, obj, $form,
675 body = document.body,
676 $body = jQuery( body );
678 ok( $body.prop("nextSibling") === null, "Make sure a null expando returns null" );
680 equal( $body.prop("foo"), "bar", "Make sure the expando is preferred over the dom attribute" );
681 body["foo"] = undefined;
682 ok( $body.prop("foo") === undefined, "Make sure the expando is preferred over the dom attribute, even if undefined" );
684 select = document.createElement("select");
685 optgroup = document.createElement("optgroup");
686 option = document.createElement("option");
688 optgroup.appendChild( option );
689 select.appendChild( optgroup );
691 equal( jQuery( option ).prop("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
692 equal( jQuery( document ).prop("nodeName"), "#document", "prop works correctly on document nodes (bug #7451)." );
694 attributeNode = document.createAttribute("irrelevant");
695 commentNode = document.createComment("some comment");
696 textNode = document.createTextNode("some text");
698 jQuery.each( [ document, attributeNode, commentNode, textNode, obj, "#firstp" ], function( i, ele ) {
699 strictEqual( jQuery( ele ).prop("nonexisting"), undefined, "prop works correctly for non existing attributes (bug #7500)." );
703 jQuery.each( [ document, obj ], function( i, ele ) {
704 var $ele = jQuery( ele );
705 $ele.prop( "nonexisting", "foo" );
706 equal( $ele.prop("nonexisting"), "foo", "prop(name, value) works correctly for non existing attributes (bug #7500)." );
708 jQuery( document ).removeProp("nonexisting");
710 $form = jQuery("#form").prop( "enctype", "multipart/form-data" );
711 equal( $form.prop("enctype"), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 #6743)" );
714 test( "prop('tabindex')", function() {
717 // inputs without tabIndex attribute
718 equal( jQuery("#inputWithoutTabIndex").prop("tabindex"), 0, "input without tabindex" );
719 equal( jQuery("#buttonWithoutTabIndex").prop("tabindex"), 0, "button without tabindex" );
720 equal( jQuery("#textareaWithoutTabIndex").prop("tabindex"), 0, "textarea without tabindex" );
722 // elements not natively tabbable
723 equal( jQuery("#listWithTabIndex").prop("tabindex"), 5, "not natively tabbable, with tabindex set to 0" );
724 equal( jQuery("#divWithNoTabIndex").prop("tabindex"), -1, "not natively tabbable, no tabindex set" );
727 equal( jQuery("#linkWithNoTabIndex").prop("tabindex"), 0, "anchor with href, no tabindex set" );
728 equal( jQuery("#linkWithTabIndex").prop("tabindex"), 2, "anchor with href, tabindex set to 2" );
729 equal( jQuery("#linkWithNegativeTabIndex").prop("tabindex"), -1, "anchor with href, tabindex set to -1" );
731 // anchor without href
732 equal( jQuery("#linkWithNoHrefWithNoTabIndex").prop("tabindex"), -1, "anchor without href, no tabindex set" );
733 equal( jQuery("#linkWithNoHrefWithTabIndex").prop("tabindex"), 1, "anchor without href, tabindex set to 2" );
734 equal( jQuery("#linkWithNoHrefWithNegativeTabIndex").prop("tabindex"), -1, "anchor without href, no tabindex set" );
737 test( "prop('tabindex', value)", 10, function() {
740 element = jQuery("#divWithNoTabIndex");
742 equal( element.prop("tabindex"), -1, "start with no tabindex" );
744 // set a positive string
745 element.prop( "tabindex", "1" );
746 equal( element.prop("tabindex"), 1, "set tabindex to 1 (string)" );
749 element.prop( "tabindex", "0" );
750 equal( element.prop("tabindex"), 0, "set tabindex to 0 (string)" );
752 // set a negative string
753 element.prop( "tabindex", "-1" );
754 equal( element.prop("tabindex"), -1, "set tabindex to -1 (string)" );
756 // set a positive number
757 element.prop( "tabindex", 1 );
758 equal( element.prop("tabindex"), 1, "set tabindex to 1 (number)" );
761 element.prop( "tabindex", 0 );
762 equal( element.prop("tabindex"), 0, "set tabindex to 0 (number)" );
764 // set a negative number
765 element.prop( "tabindex", -1 );
766 equal( element.prop("tabindex"), -1, "set tabindex to -1 (number)" );
768 element = jQuery("#linkWithTabIndex");
769 equal( element.prop("tabindex"), 2, "start with tabindex 2" );
771 element.prop( "tabindex", -1 );
772 equal( element.prop("tabindex"), -1, "set negative tabindex" );
774 clone = element.clone();
775 clone.prop( "tabindex", 1 );
776 equal( clone[ 0 ].getAttribute("tabindex"), "1", "set tabindex on cloned element" );
779 test( "removeProp(String)", function() {
781 var attributeNode = document.createAttribute("irrelevant"),
782 commentNode = document.createComment("some comment"),
783 textNode = document.createTextNode("some text"),
787 jQuery( "#firstp" ).prop( "nonexisting", "foo" ).removeProp( "nonexisting" )[ 0 ]["nonexisting"],
789 "removeprop works correctly on DOM element nodes"
792 jQuery.each( [ document, obj ], function( i, ele ) {
793 var $ele = jQuery( ele );
794 $ele.prop( "nonexisting", "foo" ).removeProp("nonexisting");
795 strictEqual( ele["nonexisting"], undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
797 jQuery.each( [ commentNode, textNode, attributeNode ], function( i, ele ) {
798 var $ele = jQuery( ele );
799 $ele.prop( "nonexisting", "foo" ).removeProp("nonexisting");
800 strictEqual( ele["nonexisting"], undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
804 test( "val() after modification", function() {
808 document.getElementById("text1").value = "bla";
809 equal( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
813 test( "val()", function() {
815 expect( 20 + ( jQuery.fn.serialize ? 6 : 0 ) );
818 equal( jQuery("#text1").val(), "Test", "Check for value of input element" );
819 // ticket #1714 this caused a JS error in IE
820 equal( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
821 ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
823 equal( jQuery("#select2").val(), "3", "Call val() on a single='single' select" );
825 deepEqual( jQuery("#select3").val(), [ "1", "2" ], "Call val() on a multiple='multiple' select" );
827 equal( jQuery("#option3c").val(), "2", "Call val() on a option element with value" );
829 equal( jQuery("#option3a").val(), "", "Call val() on a option element with empty value" );
831 equal( jQuery("#option3e").val(), "no value", "Call val() on a option element with no value attribute" );
833 equal( jQuery("#option3a").val(), "", "Call val() on a option element with no value attribute" );
835 jQuery("#select3").val("");
836 deepEqual( jQuery("#select3").val(), [""], "Call val() on a multiple='multiple' select" );
838 deepEqual( jQuery("#select4").val(), [], "Call val() on multiple='multiple' select with all disabled options" );
840 jQuery("#select4 optgroup").add("#select4 > [disabled]").attr( "disabled", false );
841 deepEqual( jQuery("#select4").val(), [ "2", "3" ], "Call val() on multiple='multiple' select with some disabled options" );
843 jQuery("#select4").attr( "disabled", true );
844 deepEqual( jQuery("#select4").val(), [ "2", "3" ], "Call val() on disabled multiple='multiple' select" );
846 equal( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
848 jQuery("#select5").val( 1 );
849 equal( jQuery("#select5").val(), "1", "Check value on ambiguous select." );
851 jQuery("#select5").val( 3 );
852 equal( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
855 jQuery("<select name='select12584' id='select12584'><option value='1' disabled='disabled'>1</option></select>").val(),
857 "Select-one with only option disabled (#12584)"
860 if ( jQuery.fn.serialize ) {
861 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");
863 deepEqual( checks.serialize(), "", "Get unchecked values." );
865 equal( checks.eq( 3 ).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
868 deepEqual( checks.serialize(), "test=2", "Get a single checked value." );
870 checks.val([ "1", "" ]);
871 deepEqual( checks.serialize(), "test=1&test=", "Get multiple checked values." );
873 checks.val([ "", "2" ]);
874 deepEqual( checks.serialize(), "test=2&test=", "Get multiple checked values." );
876 checks.val([ "1", "on" ]);
877 deepEqual( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
882 $button = jQuery("<button value='foobar'>text</button>").insertAfter("#button");
883 equal( $button.val(), "foobar", "Value retrieval on a button does not return innerHTML" );
884 equal( $button.val("baz").html(), "text", "Setting the value does not change innerHTML" );
886 equal( jQuery("<option/>").val("test").attr("value"), "test", "Setting value sets the value attribute" );
889 test("val() with non-matching values on dropdown list", function() {
892 jQuery("#select5").val( "" );
893 equal( jQuery("#select5").val(), null, "Non-matching set on select-one" );
895 var select6 = jQuery("<select multiple id=\"select6\"><option value=\"1\">A</option><option value=\"2\">B</option></select>").appendTo("#form");
896 jQuery(select6).val( "nothing" );
897 equal( jQuery(select6).val(), null, "Non-matching set (single value) on select-multiple" );
899 jQuery(select6).val( ["nothing1", "nothing2"] );
900 equal( jQuery(select6).val(), null, "Non-matching set (array of values) on select-multiple" );
905 if ( "value" in document.createElement("meter") &&
906 "value" in document.createElement("progress") ) {
908 test( "val() respects numbers without exception (Bug #9319)", function() {
912 var $meter = jQuery("<meter min='0' max='10' value='5.6'></meter>"),
913 $progress = jQuery("<progress max='10' value='1.5'></progress>");
916 equal( typeof $meter.val(), "number", "meter, returns a number and does not throw exception" );
917 equal( $meter.val(), $meter[ 0 ].value, "meter, api matches host and does not throw exception" );
919 equal( typeof $progress.val(), "number", "progress, returns a number and does not throw exception" );
920 equal( $progress.val(), $progress[ 0 ].value, "progress, api matches host and does not throw exception" );
929 var testVal = function( valueObj ) {
932 jQuery("#text1").val( valueObj("test") );
933 equal( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" );
935 jQuery("#text1").val( valueObj( undefined ) );
936 equal( document.getElementById("text1").value, "", "Check for modified (via val(undefined)) value of input element" );
938 jQuery("#text1").val( valueObj( 67 ) );
939 equal( document.getElementById("text1").value, "67", "Check for modified (via val(Number)) value of input element" );
941 jQuery("#text1").val( valueObj( null ) );
942 equal( document.getElementById("text1").value, "", "Check for modified (via val(null)) value of input element" );
945 $select = jQuery( "<select multiple><option value='1'/><option value='2'/></select>" ),
946 $select1 = jQuery("#select1");
948 $select1.val( valueObj("3") );
949 equal( $select1.val(), "3", "Check for modified (via val(String)) value of select element" );
951 $select1.val( valueObj( 2 ) );
952 equal( $select1.val(), "2", "Check for modified (via val(Number)) value of select element" );
954 $select1.append("<option value='4'>four</option>");
955 $select1.val( valueObj( 4 ) );
956 equal( $select1.val(), "4", "Should be possible to set the val() to a newly created option" );
958 // using contents will get comments regular, text, and comment nodes
959 j = jQuery("#nonnodes").contents();
960 j.val( valueObj( "asdf" ) );
961 equal( j.val(), "asdf", "Check node,textnode,comment with val()" );
962 j.removeAttr("value");
964 $select.val( valueObj( [ "1", "2" ] ) );
965 deepEqual( $select.val(), [ "1", "2" ], "Should set array of values" );
968 test( "val(String/Number)", function() {
972 test( "val(Function)", function() {
973 testVal( functionReturningObj );
976 test( "val(Array of Numbers) (Bug #7123)", function() {
978 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' />");
979 var elements = jQuery("input[name=arrayTest]").val([ 1, 2 ]);
980 ok( elements[ 0 ].checked, "First element was checked" );
981 ok( elements[ 1 ].checked, "Second element was checked" );
982 ok( !elements[ 2 ].checked, "Third element was unchecked" );
983 ok( !elements[ 3 ].checked, "Fourth element remained unchecked" );
988 test( "val(Function) with incoming value", function() {
991 var oldVal = jQuery("#text1").val();
993 jQuery("#text1").val(function( i, val ) {
994 equal( val, oldVal, "Make sure the incoming value is correct." );
998 equal( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" );
1000 oldVal = jQuery("#text1").val();
1002 jQuery("#text1").val(function( i, val ) {
1003 equal( val, oldVal, "Make sure the incoming value is correct." );
1007 equal( document.getElementById("text1").value, "67", "Check for modified (via val(Number)) value of input element" );
1009 oldVal = jQuery("#select1").val();
1011 jQuery("#select1").val(function( i, val ) {
1012 equal( val, oldVal, "Make sure the incoming value is correct." );
1016 equal( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
1018 oldVal = jQuery("#select1").val();
1020 jQuery("#select1").val(function( i, val ) {
1021 equal( val, oldVal, "Make sure the incoming value is correct." );
1025 equal( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
1027 jQuery("#select1").append("<option value='4'>four</option>");
1029 oldVal = jQuery("#select1").val();
1031 jQuery("#select1").val(function( i, val ) {
1032 equal( val, oldVal, "Make sure the incoming value is correct." );
1036 equal( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
1039 // testing if a form.reset() breaks a subsequent call to a select element's .val() (in IE only)
1040 test( "val(select) after form.reset() (Bug #2551)", function() {
1043 jQuery("<form id='kk' name='kk'><select id='kkk'><option value='cf'>cf</option><option value='gf'>gf</option></select></form>").appendTo("#qunit-fixture");
1045 jQuery("#kkk").val("gf");
1047 document["kk"].reset();
1049 equal( jQuery("#kkk")[ 0 ].value, "cf", "Check value of select after form reset." );
1050 equal( jQuery("#kkk").val(), "cf", "Check value of select after form reset." );
1052 // re-verify the multi-select is not broken (after form.reset) by our fix for single-select
1053 deepEqual( jQuery("#select3").val(), ["1", "2"], "Call val() on a multiple='multiple' select" );
1055 jQuery("#kk").remove();
1058 var testAddClass = function( valueObj ) {
1062 div = jQuery("#qunit-fixture div");
1063 div.addClass( valueObj("test") );
1065 for ( i = 0; i < div.length; i++ ) {
1066 if ( !~div.get( i ).className.indexOf("test") ) {
1070 ok( pass, "Add Class" );
1072 // using contents will get regular, text, and comment nodes
1073 j = jQuery("#nonnodes").contents();
1074 j.addClass( valueObj("asdf") );
1075 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
1077 div = jQuery("<div/>");
1079 div.addClass( valueObj("test") );
1080 equal( div.attr("class"), "test", "Make sure there's no extra whitespace." );
1082 div.attr( "class", " foo" );
1083 div.addClass( valueObj("test") );
1084 equal( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
1086 div.attr( "class", "foo" );
1087 div.addClass( valueObj("bar baz") );
1088 equal( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
1091 div.addClass( valueObj("foo") ).addClass( valueObj("foo") );
1092 equal( div.attr("class"), "foo", "Do not add the same class twice in separate calls." );
1094 div.addClass( valueObj("fo") );
1095 equal( div.attr("class"), "foo fo", "Adding a similar class does not get interrupted." );
1096 div.removeClass().addClass("wrap2");
1097 ok( div.addClass("wrap").hasClass("wrap"), "Can add similarly named classes");
1100 div.addClass( valueObj("bar bar") );
1101 equal( div.attr("class"), "bar", "Do not add the same class twice in the same call." );
1104 test( "addClass(String)", function() {
1105 testAddClass( bareObj );
1108 test( "addClass(Function)", function() {
1109 testAddClass( functionReturningObj );
1112 test( "addClass(Function) with incoming value", function() {
1115 div = jQuery("#qunit-fixture div"),
1116 old = div.map(function() {
1117 return jQuery(this).attr("class") || "";
1120 div.addClass(function( i, val ) {
1121 if ( this.id !== "_firebugConsole" ) {
1122 equal( val, old[ i ], "Make sure the incoming value is correct." );
1128 for ( i = 0; i < div.length; i++ ) {
1129 if ( div.get(i).className.indexOf("test") === -1 ) {
1133 ok( pass, "Add Class" );
1136 var testRemoveClass = function(valueObj) {
1139 var $set = jQuery("#qunit-fixture div"),
1140 div = document.createElement("div");
1142 $set.addClass("test").removeClass( valueObj("test") );
1144 ok( !$set.is(".test"), "Remove Class" );
1146 $set.addClass("test").addClass("foo").addClass("bar");
1147 $set.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
1149 ok( !$set.is(".test,.bar,.foo"), "Remove multiple classes" );
1151 // Make sure that a null value doesn't cause problems
1152 $set.eq( 0 ).addClass("expected").removeClass( valueObj( null ) );
1153 ok( $set.eq( 0 ).is(".expected"), "Null value passed to removeClass" );
1155 $set.eq( 0 ).addClass("expected").removeClass( valueObj("") );
1156 ok( $set.eq( 0 ).is(".expected"), "Empty string passed to removeClass" );
1158 // using contents will get regular, text, and comment nodes
1159 $set = jQuery("#nonnodes").contents();
1160 $set.removeClass( valueObj("asdf") );
1161 ok( !$set.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
1164 jQuery( div ).removeClass( valueObj("foo") );
1165 strictEqual( jQuery( div ).attr("class"), undefined, "removeClass doesn't create a class attribute" );
1167 div.className = " test foo ";
1169 jQuery( div ).removeClass( valueObj("foo") );
1170 equal( div.className, "test", "Make sure remaining className is trimmed." );
1172 div.className = " test ";
1174 jQuery( div ).removeClass( valueObj("test") );
1175 equal( div.className, "", "Make sure there is nothing left after everything is removed." );
1178 test( "removeClass(String) - simple", function() {
1179 testRemoveClass( bareObj );
1182 test( "removeClass(Function) - simple", function() {
1183 testRemoveClass( functionReturningObj );
1186 test( "removeClass(Function) with incoming value", function() {
1189 var $divs = jQuery("#qunit-fixture div").addClass("test"), old = $divs.map(function() {
1190 return jQuery( this ).attr("class");
1193 $divs.removeClass(function( i, val ) {
1194 if ( this.id !== "_firebugConsole" ) {
1195 equal( val, old[ i ], "Make sure the incoming value is correct." );
1200 ok( !$divs.is(".test"), "Remove Class" );
1203 test( "removeClass() removes duplicates", function() {
1206 var $div = jQuery( jQuery.parseHTML("<div class='x x x'></div>") );
1208 $div.removeClass("x");
1210 ok( !$div.hasClass("x"), "Element with multiple same classes does not escape the wrath of removeClass()" );
1213 test("removeClass(undefined) is a no-op", function() {
1216 var $div = jQuery("<div class='base second'></div>");
1217 $div.removeClass( undefined );
1219 ok( $div.hasClass("base") && $div.hasClass("second"), "Element still has classes after removeClass(undefined)" );
1222 var testToggleClass = function(valueObj) {
1225 var e = jQuery("#firstp");
1226 ok( !e.is(".test"), "Assert class not present" );
1227 e.toggleClass( valueObj("test") );
1228 ok( e.is(".test"), "Assert class present" );
1229 e.toggleClass( valueObj("test") );
1230 ok( !e.is(".test"), "Assert class not present" );
1232 // class name with a boolean
1233 e.toggleClass( valueObj("test"), false );
1234 ok( !e.is(".test"), "Assert class not present" );
1235 e.toggleClass( valueObj("test"), true );
1236 ok( e.is(".test"), "Assert class present" );
1237 e.toggleClass( valueObj("test"), false );
1238 ok( !e.is(".test"), "Assert class not present" );
1240 // multiple class names
1241 e.addClass("testA testB");
1242 ok( e.is(".testA.testB"), "Assert 2 different classes present" );
1243 e.toggleClass( valueObj("testB testC") );
1244 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
1245 e.toggleClass( valueObj("testA testC") );
1246 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
1248 // toggleClass storage
1249 e.toggleClass( true );
1250 ok( e[ 0 ].className === "", "Assert class is empty (data was empty)" );
1251 e.addClass("testD testE");
1252 ok( e.is(".testD.testE"), "Assert class present" );
1254 ok( !e.is(".testD.testE"), "Assert class not present" );
1255 ok( jQuery._data(e[ 0 ], "__className__") === "testD testE", "Assert data was stored" );
1257 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
1258 e.toggleClass( false );
1259 ok( !e.is(".testD.testE"), "Assert class not present" );
1260 e.toggleClass( true );
1261 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
1263 e.toggleClass( false );
1265 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
1268 e.removeClass("testD");
1269 QUnit.expectJqData( this, e[ 0 ], "__className__" );
1272 test( "toggleClass(String|boolean|undefined[, boolean])", function() {
1273 testToggleClass( bareObj );
1276 test( "toggleClass(Function[, boolean])", function() {
1277 testToggleClass( functionReturningObj );
1280 test( "toggleClass(Function[, boolean]) with incoming value", function() {
1283 var e = jQuery("#firstp"),
1284 old = e.attr("class") || "";
1286 ok( !e.is(".test"), "Assert class not present" );
1288 e.toggleClass(function( i, val ) {
1289 equal( old, val, "Make sure the incoming value is correct." );
1292 ok( e.is(".test"), "Assert class present" );
1294 old = e.attr("class");
1296 e.toggleClass(function( i, val ) {
1297 equal( old, val, "Make sure the incoming value is correct." );
1300 ok( !e.is(".test"), "Assert class not present" );
1302 old = e.attr("class") || "";
1304 // class name with a boolean
1305 e.toggleClass(function( i, val, state ) {
1306 equal( old, val, "Make sure the incoming value is correct." );
1307 equal( state, false, "Make sure that the state is passed in." );
1310 ok( !e.is(".test"), "Assert class not present" );
1312 old = e.attr("class") || "";
1314 e.toggleClass(function( i, val, state ) {
1315 equal( old, val, "Make sure the incoming value is correct." );
1316 equal( state, true, "Make sure that the state is passed in." );
1319 ok( e.is(".test"), "Assert class present" );
1321 old = e.attr("class");
1323 e.toggleClass(function( i, val, state ) {
1324 equal( old, val, "Make sure the incoming value is correct." );
1325 equal( state, false, "Make sure that the state is passed in." );
1328 ok( !e.is(".test"), "Assert class not present" );
1331 test( "addClass, removeClass, hasClass", function() {
1334 var jq = jQuery("<p>Hi</p>"), x = jq[ 0 ];
1337 equal( x.className, "hi", "Check single added class" );
1339 jq.addClass("foo bar");
1340 equal( x.className, "hi foo bar", "Check more added classes" );
1343 equal( x.className, "", "Remove all classes" );
1345 jq.addClass("hi foo bar");
1346 jq.removeClass("foo");
1347 equal( x.className, "hi bar", "Check removal of one class" );
1349 ok( jq.hasClass("hi"), "Check has1" );
1350 ok( jq.hasClass("bar"), "Check has2" );
1352 jq = jQuery("<p class='class1\nclass2\tcla.ss3\n\rclass4'></p>");
1354 ok( jq.hasClass("class1"), "Check hasClass with line feed" );
1355 ok( jq.is(".class1"), "Check is with line feed" );
1356 ok( jq.hasClass("class2"), "Check hasClass with tab" );
1357 ok( jq.is(".class2"), "Check is with tab" );
1358 ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
1359 ok( jq.hasClass("class4"), "Check hasClass with carriage return" );
1360 ok( jq.is(".class4"), "Check is with carriage return" );
1362 jq.removeClass("class2");
1363 ok( jq.hasClass("class2") === false, "Check the class has been properly removed" );
1364 jq.removeClass("cla");
1365 ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
1366 jq.removeClass("cla.ss3");
1367 ok( jq.hasClass("cla.ss3") === false, "Check the dotted class has been removed" );
1368 jq.removeClass("class4");
1369 ok( jq.hasClass("class4") === false, "Check the class has been properly removed" );
1372 test( "addClass, removeClass, hasClass on many elements", function() {
1375 var elem = jQuery( "<p>p0</p><p>p1</p><p>p2</p>" );
1377 elem.addClass( "hi" );
1378 equal( elem[ 0 ].className, "hi", "Check single added class" );
1379 equal( elem[ 1 ].className, "hi", "Check single added class" );
1380 equal( elem[ 2 ].className, "hi", "Check single added class" );
1382 elem.addClass( "foo bar" );
1383 equal( elem[ 0 ].className, "hi foo bar", "Check more added classes" );
1384 equal( elem[ 1 ].className, "hi foo bar", "Check more added classes" );
1385 equal( elem[ 2 ].className, "hi foo bar", "Check more added classes" );
1388 equal( elem[ 0 ].className, "", "Remove all classes" );
1389 equal( elem[ 1 ].className, "", "Remove all classes" );
1390 equal( elem[ 2 ].className, "", "Remove all classes" );
1392 elem.addClass( "hi foo bar" );
1393 elem.removeClass( "foo" );
1394 equal( elem[ 0 ].className, "hi bar", "Check removal of one class" );
1395 equal( elem[ 1 ].className, "hi bar", "Check removal of one class" );
1396 equal( elem[ 2 ].className, "hi bar", "Check removal of one class" );
1398 ok( elem.hasClass( "hi" ), "Check has1" );
1399 ok( elem.hasClass( "bar" ), "Check has2" );
1401 ok( jQuery( "<p class='hi'>p0</p><p>p1</p><p>p2</p>" ).hasClass( "hi" ),
1402 "Did find a class in the first element" );
1403 ok( jQuery( "<p>p0</p><p class='hi'>p1</p><p>p2</p>" ).hasClass( "hi" ),
1404 "Did find a class in the second element" );
1405 ok( jQuery( "<p>p0</p><p>p1</p><p class='hi'>p2</p>" ).hasClass( "hi" ),
1406 "Did find a class in the last element" );
1408 ok( jQuery( "<p class='hi'>p0</p><p class='hi'>p1</p><p class='hi'>p2</p>" ).hasClass( "hi" ),
1409 "Did find a class when present in all elements" );
1411 ok( !jQuery( "<p class='hi0'>p0</p><p class='hi1'>p1</p><p class='hi2'>p2</p>" ).hasClass( "hi" ),
1412 "Did not find a class when not present" );
1415 test( "contents().hasClass() returns correct values", function() {
1418 var $div = jQuery("<div><span class='foo'></span><!-- comment -->text</div>"),
1419 $contents = $div.contents();
1421 ok( $contents.hasClass("foo"), "Found 'foo' in $contents" );
1422 ok( !$contents.hasClass("undefined"), "Did not find 'undefined' in $contents (correctly)" );
1425 test( "hasClass correctly interprets non-space separators (#13835)", function() {
1431 "line-feed": " ",
1432 "form-feed": "",
1433 "carriage-return": " "
1435 classes = jQuery.map( map, function( separator, label ) {
1436 return " " + separator + label + separator + " ";
1438 $div = jQuery( "<div class='" + classes + "'></div>" );
1440 jQuery.each( map, function( label ) {
1441 ok( $div.hasClass( label ), label.replace( "-", " " ) );
1445 test( "coords returns correct values in IE6/IE7, see #10828", function() {
1449 map = jQuery("<map />");
1451 area = map.html("<area shape='rect' coords='0,0,0,0' href='#' alt='a' />").find("area");
1452 equal( area.attr("coords"), "0,0,0,0", "did not retrieve coords correctly" );
1455 test( "should not throw at $(option).val() (#14686)", 1, function() {
1457 jQuery( "<option/>" ).val();
1464 test( "option value not trimmed when setting via parent select", function() {
1466 equal( jQuery( "<select><option> 2</option></select>" ).val( "2" ).val(), "2" );
1469 test( "Insignificant white space returned for $(option).val() (#14858)", function() {
1472 var val = jQuery( "<option></option>" ).val();
1473 equal( val.length, 0, "Empty option should have no value" );
1475 val = jQuery( "<option> </option>" ).val();
1476 equal( val.length, 0, "insignificant white-space returned for value" );
1478 val = jQuery( "<option> test </option>" ).val();
1479 equal( val.length, 4, "insignificant white-space returned for value" );