1 module("attributes", { teardown: moduleTeardown });
3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
7 test("jQuery.attrFix/jQuery.propFix integrity test", function() {
10 // This must be maintained and equal jQuery.attrFix when appropriate
11 // Ensure that accidental or erroneous property
12 // overwrites don't occur
13 // This is simply for better code coverage and future proofing.
19 maxlength: "maxLength",
20 cellspacing: "cellSpacing",
21 cellpadding: "cellPadding",
25 frameborder: "frameBorder",
26 contenteditable: "contentEditable"
33 deepEqual(propsShouldBe, jQuery.attrFix, "jQuery.attrFix passes integrity check");
34 deepEqual(props, jQuery.propFix, "jQuery.propFix passes integrity check");
37 test("attr(String)", function() {
40 equals( jQuery("#text1").attr("type"), "text", "Check for type attribute" );
41 equals( jQuery("#radio1").attr("type"), "radio", "Check for type attribute" );
42 equals( jQuery("#check1").attr("type"), "checkbox", "Check for type attribute" );
43 equals( jQuery("#simon1").attr("rel"), "bookmark", "Check for rel attribute" );
44 equals( jQuery("#google").attr("title"), "Google!", "Check for title attribute" );
45 equals( jQuery("#mark").attr("hreflang"), "en", "Check for hreflang attribute" );
46 equals( jQuery("#en").attr("lang"), "en", "Check for lang attribute" );
47 equals( jQuery("#simon").attr("class"), "blog link", "Check for class attribute" );
48 equals( jQuery("#name").attr("name"), "name", "Check for name attribute" );
49 equals( jQuery("#text1").attr("name"), "action", "Check for name attribute" );
50 ok( jQuery("#form").attr("action").indexOf("formaction") >= 0, "Check for action attribute" );
51 equals( jQuery("#text1").attr("value", "t").attr("value"), "t", "Check setting the value attribute" );
52 equals( jQuery("<div value='t'></div>").attr("value"), "t", "Check setting custom attr named 'value' on a div" );
53 equals( jQuery("#form").attr("blah", "blah").attr("blah"), "blah", "Set non-existant attribute on a form" );
54 equals( jQuery("#foo").attr("height"), undefined, "Non existent height attribute should return undefined" );
56 // [7472] & [3113] (form contains an input with name="action" or name="id")
57 var extras = jQuery("<input name='id' name='name' /><input id='target' name='target' />").appendTo("#testForm");
58 equals( jQuery("#form").attr("action","newformaction").attr("action"), "newformaction", "Check that action attribute was changed" );
59 equals( jQuery("#testForm").attr("target"), undefined, "Retrieving target does not equal the input with name=target" );
60 equals( jQuery("#testForm").attr("target", "newTarget").attr("target"), "newTarget", "Set target successfully on a form" );
61 equals( jQuery("#testForm").removeAttr("id").attr("id"), undefined, "Retrieving id does not equal the input with name=id after id is removed [#7472]" );
62 // Bug #3685 (form contains input with name="name")
63 equals( jQuery("#testForm").attr("name"), undefined, "Retrieving name does not retrieve input with name=name" );
66 equals( jQuery("#text1").attr("maxlength"), "30", "Check for maxlength attribute" );
67 equals( jQuery("#text1").attr("maxLength"), "30", "Check for maxLength attribute" );
68 equals( jQuery("#area1").attr("maxLength"), "30", "Check for maxLength attribute" );
70 // using innerHTML in IE causes href attribute to be serialized to the full path
71 jQuery("<a/>").attr({ "id": "tAnchor5", "href": "#5" }).appendTo("#qunit-fixture");
72 equals( jQuery("#tAnchor5").attr("href"), "#5", "Check for non-absolute href (an anchor)" );
74 // list attribute is readonly by default in browsers that support it
75 jQuery("#list-test").attr("list", "datalist");
76 equals( jQuery("#list-test").attr("list"), "datalist", "Check setting list attribute" );
78 // Related to [5574] and [5683]
79 var body = document.body, $body = jQuery(body);
81 strictEqual( $body.attr("foo"), undefined, "Make sure that a non existent attribute returns undefined" );
83 body.setAttribute("foo", "baz");
84 equals( $body.attr("foo"), "baz", "Make sure the dom attribute is retrieved when no expando is found" );
86 $body.attr("foo","cool");
87 equals( $body.attr("foo"), "cool", "Make sure that setting works well when both expando and dom attribute are available" );
89 body.removeAttribute("foo"); // Cleanup
91 var select = document.createElement("select"), optgroup = document.createElement("optgroup"), option = document.createElement("option");
92 optgroup.appendChild( option );
93 select.appendChild( optgroup );
95 equal( jQuery( option ).attr("selected"), "selected", "Make sure that a single option is selected, even when in an optgroup." );
97 var $img = jQuery("<img style='display:none' width='215' height='53' src='http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif'/>").appendTo("body");
98 equals( $img.attr("width"), "215", "Retrieve width attribute an an element with display:none." );
99 equals( $img.attr("height"), "53", "Retrieve height attribute an an element with display:none." );
101 // Check for style support
102 ok( !!~jQuery("#dl").attr("style").indexOf("position"), "Check style attribute getter, also normalize css props to lowercase" );
103 ok( !!~jQuery("#foo").attr("style", "position:absolute;").attr("style").indexOf("position"), "Check style setter" );
105 // Check value on button element (#1954)
106 var $button = jQuery("<button value='foobar'>text</button>").insertAfter("#button");
107 equals( $button.attr("value"), "foobar", "Value retrieval on a button does not return innerHTML" );
108 equals( $button.attr("value", "baz").html(), "text", "Setting the value does not change innerHTML" );
110 // Attributes with a colon on a table element (#1591)
111 equals( jQuery("#table").attr("test:attrib"), undefined, "Retrieving a non-existent attribute on a table with a colon does not throw an error." );
112 equals( jQuery("#table").attr("test:attrib", "foobar").attr("test:attrib"), "foobar", "Setting an attribute on a table with a colon does not throw an error." );
114 var $form = jQuery("<form class='something'></form>").appendTo("#qunit-fixture");
115 equal( $form.attr("class"), "something", "Retrieve the class attribute on a form." );
117 var $a = jQuery("<a href='#' onclick='something()'>Click</a>").appendTo("#qunit-fixture");
118 equal( $a.attr("onclick"), "something()", "Retrieve ^on attribute without anonymous function wrapper." );
120 ok( jQuery("<div/>").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." );
121 ok( jQuery("<div/>").attr("title") === undefined, "Make sure undefined is returned when no attribute is found." );
122 equal( jQuery("<div/>").attr("title", "something").attr("title"), "something", "Set the title attribute." );
123 ok( jQuery().attr("doesntexist") === undefined, "Make sure undefined is returned when no element is there." );
124 equal( jQuery("<div/>").attr("value"), undefined, "An unset value on a div returns undefined." );
125 equal( jQuery("<input/>").attr("value"), "", "An unset value on an input returns current value." );
129 test("attr(String) in XML Files", function() {
132 jQuery.get("data/dashboard.xml", function( xml ) {
133 equal( jQuery( "locations", xml ).attr("class"), "foo", "Check class attribute in XML document" );
134 equal( jQuery( "location", xml ).attr("for"), "bar", "Check for attribute in XML document" );
135 equal( jQuery( "location", xml ).attr("checked"), "different", "Check that hooks are not attached in XML document" );
141 test("attr(String, Function)", function() {
143 equals( jQuery("#text1").attr("value", function() { return this.id; })[0].value, "text1", "Set value from id" );
144 equals( jQuery("#text1").attr("title", function(i) { return i; }).attr("title"), "0", "Set value with an index");
147 test("attr(Hash)", function() {
150 jQuery("div").attr({foo: "baz", zoo: "ping"}).each(function(){
151 if ( this.getAttribute("foo") != "baz" && this.getAttribute("zoo") != "ping" ) pass = false;
153 ok( pass, "Set Multiple Attributes" );
154 equals( jQuery("#text1").attr({value: function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
155 equals( jQuery("#text1").attr({title: function(i) { return i; }}).attr("title"), "0", "Set attribute to computed value #2");
159 test("attr(String, Object)", function() {
162 var div = jQuery("div").attr("foo", "bar"),
165 for ( var i = 0; i < div.size(); i++ ) {
166 if ( div.get(i).getAttribute("foo") != "bar" ){
172 equals( fail, false, "Set Attribute, the #" + fail + " element didn't get the attribute 'foo'" );
174 ok( jQuery("#foo").attr({ "width": null }), "Try to set an attribute to nothing" );
176 jQuery("#name").attr("name", "something");
177 equals( jQuery("#name").attr("name"), "something", "Set name attribute" );
178 jQuery("#name").attr("name", null);
179 equals( jQuery("#name").attr("name"), undefined, "Remove name attribute" );
180 var $input = jQuery("<input>", { name: "something" });
181 equals( $input.attr("name"), "something", "Check element creation gets/sets the name attribute." );
183 jQuery("#check2").prop("checked", true).prop("checked", false).attr("checked", true);
184 equals( document.getElementById("check2").checked, true, "Set checked attribute" );
185 equals( jQuery("#check2").prop("checked"), true, "Set checked attribute" );
186 equals( jQuery("#check2").attr("checked"), "checked", "Set checked attribute" );
187 jQuery("#check2").attr("checked", false);
188 equals( document.getElementById("check2").checked, false, "Set checked attribute" );
189 equals( jQuery("#check2").prop("checked"), false, "Set checked attribute" );
190 equals( jQuery("#check2").attr("checked"), undefined, "Set checked attribute" );
191 jQuery("#text1").attr("readonly", true);
192 equals( document.getElementById("text1").readOnly, true, "Set readonly attribute" );
193 equals( jQuery("#text1").prop("readOnly"), true, "Set readonly attribute" );
194 equals( jQuery("#text1").attr("readonly"), "readonly", "Set readonly attribute" );
195 jQuery("#text1").attr("readonly", false);
196 equals( document.getElementById("text1").readOnly, false, "Set readonly attribute" );
197 equals( jQuery("#text1").prop("readOnly"), false, "Set readonly attribute" );
198 equals( jQuery("#text1").attr("readonly"), undefined, "Set readonly attribute" );
200 jQuery("#check2").prop("checked", true);
201 equals( document.getElementById("check2").checked, true, "Set checked attribute" );
202 equals( jQuery("#check2").prop("checked"), true, "Set checked attribute" );
203 equals( jQuery("#check2").attr("checked"), "checked", "Set checked attribute" );
204 jQuery("#check2").prop("checked", false);
205 equals( document.getElementById("check2").checked, false, "Set checked attribute" );
206 equals( jQuery("#check2").prop("checked"), false, "Set checked attribute" );
207 equals( jQuery("#check2").attr("checked"), undefined, "Set checked attribute" );
209 jQuery("#check2").attr("checked", "checked");
210 equal( document.getElementById("check2").checked, true, "Set checked attribute with 'checked'" );
211 equal( jQuery("#check2").prop("checked"), true, "Set checked attribute" );
212 equal( jQuery("#check2").attr("checked"), "checked", "Set checked attribute" );
214 jQuery("#text1").prop("readOnly", true);
215 equals( document.getElementById("text1").readOnly, true, "Set readonly attribute" );
216 equals( jQuery("#text1").prop("readOnly"), true, "Set readonly attribute" );
217 equals( jQuery("#text1").attr("readonly"), "readonly", "Set readonly attribute" );
218 jQuery("#text1").prop("readOnly", false);
219 equals( document.getElementById("text1").readOnly, false, "Set readonly attribute" );
220 equals( jQuery("#text1").prop("readOnly"), false, "Set readonly attribute" );
221 equals( jQuery("#text1").attr("readonly"), undefined, "Set readonly attribute" );
223 jQuery("#name").attr("maxlength", "5");
224 equals( document.getElementById("name").maxLength, 5, "Set maxlength attribute" );
225 jQuery("#name").attr("maxLength", "10");
226 equals( document.getElementById("name").maxLength, 10, "Set maxlength attribute" );
228 // HTML5 boolean attributes
229 var $text = jQuery("#text1").attr({
233 equal( $text.attr("autofocus"), "autofocus", "Set boolean attributes to the same name" );
234 equal( $text.attr("autofocus", false).attr("autofocus"), undefined, "Setting autofocus attribute to false removes it" );
235 equal( $text.attr("required"), "required", "Set boolean attributes to the same name" );
236 equal( $text.attr("required", false).attr("required"), undefined, "Setting required attribute to false removes it" );
238 var $details = jQuery("<details open></details>").appendTo("#qunit-fixture");
239 equal( $details.attr("open"), "open", "open attribute presense indicates true" );
240 equal( $details.attr("open", false).attr("open"), undefined, "Setting open attribute to false removes it" );
242 $text.attr("data-something", true);
243 equal( $text.attr("data-something"), "true", "Set data attributes");
244 equal( $text.data("something"), true, "Setting data attributes are not affected by boolean settings");
245 $text.attr("data-another", false);
246 equal( $text.attr("data-another"), "false", "Set data attributes");
247 equal( $text.data("another"), false, "Setting data attributes are not affected by boolean settings" );
248 equal( $text.attr("aria-disabled", false).attr("aria-disabled"), "false", "Setting aria attributes are not affected by boolean settings");
249 $text.removeData("something").removeData("another").removeAttr("aria-disabled");
251 jQuery("#foo").attr("contenteditable", true);
252 equals( jQuery("#foo").attr("contenteditable"), "true", "Enumerated attributes are set properly" );
254 var attributeNode = document.createAttribute("irrelevant"),
255 commentNode = document.createComment("some comment"),
256 textNode = document.createTextNode("some text"),
259 jQuery.each( [commentNode, textNode, attributeNode], function( i, elem ) {
260 var $elem = jQuery( elem );
261 $elem.attr( "nonexisting", "foo" );
262 strictEqual( $elem.attr("nonexisting"), undefined, "attr(name, value) works correctly on comment and text nodes (bug #7500)." );
265 jQuery.each( [window, document, obj, "#firstp"], function( i, elem ) {
266 var $elem = jQuery( elem );
267 strictEqual( $elem.attr("nonexisting"), undefined, "attr works correctly for non existing attributes (bug #7500)." );
268 equal( $elem.attr("something", "foo" ).attr("something"), "foo", "attr falls back to prop on unsupported arguments" );
271 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>"),
272 td = table.find("td:first");
273 td.attr("rowspan", "2");
274 equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
275 td.attr("colspan", "2");
276 equals( td[0].colSpan, 2, "Check colspan is correctly set" );
277 table.attr("cellspacing", "2");
278 equals( table[0].cellSpacing, "2", "Check cellspacing is correctly set" );
280 equals( jQuery("#area1").attr("value"), "foobar", "Value attribute retrieves the property for backwards compatibility." );
283 jQuery("#name").attr("someAttr", "0");
284 equals( jQuery("#name").attr("someAttr"), "0", "Set attribute to a string of \"0\"" );
285 jQuery("#name").attr("someAttr", 0);
286 equals( jQuery("#name").attr("someAttr"), "0", "Set attribute to the number 0" );
287 jQuery("#name").attr("someAttr", 1);
288 equals( jQuery("#name").attr("someAttr"), "1", "Set attribute to the number 1" );
290 // using contents will get comments regular, text, and comment nodes
291 var j = jQuery("#nonnodes").contents();
293 j.attr("name", "attrvalue");
294 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
295 j.removeAttr("name");
300 var type = jQuery("#check2").attr("type");
303 jQuery("#check2").attr("type","hidden");
307 ok( thrown, "Exception thrown when trying to change type property" );
308 equals( type, jQuery("#check2").attr("type"), "Verify that you can't change the type of an input element" );
310 var check = document.createElement("input");
313 jQuery(check).attr("type", "checkbox");
317 ok( thrown, "Exception thrown when trying to change type property" );
318 equals( "checkbox", jQuery(check).attr("type"), "Verify that you can change the type of an input element that isn't in the DOM" );
320 var check = jQuery("<input />");
323 check.attr("type","checkbox");
327 ok( thrown, "Exception thrown when trying to change type property" );
328 equals( "checkbox", check.attr("type"), "Verify that you can change the type of an input element that isn't in the DOM" );
330 var button = jQuery("#button");
333 button.attr("type","submit");
337 ok( thrown, "Exception thrown when trying to change type property" );
338 equals( "button", button.attr("type"), "Verify that you can't change the type of a button element" );
340 var $radio = jQuery("<input>", { "value": "sup", "type": "radio" }).appendTo("#testForm");
341 equals( $radio.val(), "sup", "Value is not reset when type is set after value on a radio" );
343 // Setting attributes on svg elements (bug #3116)
344 var $svg = jQuery("<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'>"
345 + "<circle cx='200' cy='200' r='150' />"
346 + "</svg>").appendTo("body");
347 equals( $svg.attr("cx", 100).attr("cx"), "100", "Set attribute on svg element" );
351 test("attr(jquery_method)", function(){
354 var $elem = jQuery("<div />"),
358 $elem.attr({html: "foo"}, true);
359 equals( elem.innerHTML, "foo", "attr(html)");
361 $elem.attr({text: "bar"}, true);
362 equals( elem.innerHTML, "bar", "attr(text)");
364 $elem.attr({css: {color: "red"}}, true);
365 ok( /^(#ff0000|red)$/i.test(elem.style.color), "attr(css)");
367 $elem.attr({height: 10}, true);
368 equals( elem.style.height, "10px", "attr(height)");
370 // Multiple attributes
374 css:{ paddingLeft:1, paddingRight:1 }
377 equals( elem.style.width, "10px", "attr({...})");
378 equals( elem.style.paddingLeft, "1px", "attr({...})");
379 equals( elem.style.paddingRight, "1px", "attr({...})");
383 test("attr(String, Object) - Loaded via XML document", function() {
386 jQuery.get("data/dashboard.xml", function( xml ) {
388 jQuery( "tab", xml ).each(function() {
389 titles.push( jQuery(this).attr("title") );
391 equals( titles[0], "Location", "attr() in XML context: Check first title" );
392 equals( titles[1], "Users", "attr() in XML context: Check second title" );
398 test("attr('tabindex')", function() {
401 // elements not natively tabbable
402 equals(jQuery("#listWithTabIndex").attr("tabindex"), 5, "not natively tabbable, with tabindex set to 0");
403 equals(jQuery("#divWithNoTabIndex").attr("tabindex"), undefined, "not natively tabbable, no tabindex set");
406 equals(jQuery("#linkWithNoTabIndex").attr("tabindex"), 0, "anchor with href, no tabindex set");
407 equals(jQuery("#linkWithTabIndex").attr("tabindex"), 2, "anchor with href, tabindex set to 2");
408 equals(jQuery("#linkWithNegativeTabIndex").attr("tabindex"), -1, "anchor with href, tabindex set to -1");
410 // anchor without href
411 equals(jQuery("#linkWithNoHrefWithNoTabIndex").attr("tabindex"), undefined, "anchor without href, no tabindex set");
412 equals(jQuery("#linkWithNoHrefWithTabIndex").attr("tabindex"), 1, "anchor without href, tabindex set to 2");
413 equals(jQuery("#linkWithNoHrefWithNegativeTabIndex").attr("tabindex"), -1, "anchor without href, no tabindex set");
416 test("attr('tabindex', value)", function() {
419 var element = jQuery("#divWithNoTabIndex");
420 equals(element.attr("tabindex"), undefined, "start with no tabindex");
422 // set a positive string
423 element.attr("tabindex", "1");
424 equals(element.attr("tabindex"), 1, "set tabindex to 1 (string)");
427 element.attr("tabindex", "0");
428 equals(element.attr("tabindex"), 0, "set tabindex to 0 (string)");
430 // set a negative string
431 element.attr("tabindex", "-1");
432 equals(element.attr("tabindex"), -1, "set tabindex to -1 (string)");
434 // set a positive number
435 element.attr("tabindex", 1);
436 equals(element.attr("tabindex"), 1, "set tabindex to 1 (number)");
439 element.attr("tabindex", 0);
440 equals(element.attr("tabindex"), 0, "set tabindex to 0 (number)");
442 // set a negative number
443 element.attr("tabindex", -1);
444 equals(element.attr("tabindex"), -1, "set tabindex to -1 (number)");
446 element = jQuery("#linkWithTabIndex");
447 equals(element.attr("tabindex"), 2, "start with tabindex 2");
449 element.attr("tabindex", -1);
450 equals(element.attr("tabindex"), -1, "set negative tabindex");
453 test("removeAttr(String)", function() {
455 equal( jQuery("#mark").removeAttr( "class" )[0].className, "", "remove class" );
456 equal( jQuery("#form").removeAttr("id").attr("id"), undefined, "Remove id" );
457 equal( jQuery("#foo").attr("style", "position:absolute;").removeAttr("style").attr("style"), undefined, "Check removing style attribute" );
458 equal( jQuery("#form").attr("style", "position:absolute;").removeAttr("style").attr("style"), undefined, "Check removing style attribute on a form" );
459 equal( jQuery("<div style='position: absolute'></div>").appendTo("#foo").removeAttr("style").prop("style").cssText, "", "Check removing style attribute (#9699 Webkit)" );
460 equal( jQuery("#fx-test-group").attr("height", "3px").removeAttr("height").css("height"), "1px", "Removing height attribute has no effect on height set with style attribute" );
462 jQuery("#check1").removeAttr("checked").prop("checked", true).removeAttr("checked");
463 equal( document.getElementById("check1").checked, false, "removeAttr sets boolean properties to false" );
464 jQuery("#text1").prop("readOnly", true).removeAttr("readonly");
465 equal( document.getElementById("text1").readOnly, false, "removeAttr sets boolean properties to false" );
468 test("prop(String, Object)", function() {
471 equals( jQuery("#text1").prop("value"), "Test", "Check for value attribute" );
472 equals( jQuery("#text1").prop("value", "Test2").prop("defaultValue"), "Test", "Check for defaultValue attribute" );
473 equals( jQuery("#select2").prop("selectedIndex"), 3, "Check for selectedIndex attribute" );
474 equals( jQuery("#foo").prop("nodeName").toUpperCase(), "DIV", "Check for nodeName attribute" );
475 equals( jQuery("#foo").prop("tagName").toUpperCase(), "DIV", "Check for tagName attribute" );
476 equals( jQuery("<option/>").prop("selected"), false, "Check selected attribute on disconnected element." );
478 equals( jQuery("#listWithTabIndex").prop("tabindex"), 5, "Check retrieving tabindex" );
479 jQuery("#text1").prop("readonly", true);
480 equals( document.getElementById("text1").readOnly, true, "Check setting readOnly property with 'readonly'" );
481 equals( jQuery("#label-for").prop("for"), "action", "Check retrieving htmlFor" );
482 jQuery("#text1").prop("class", "test");
483 equals( document.getElementById("text1").className, "test", "Check setting className with 'class'" );
484 equals( jQuery("#text1").prop("maxlength"), 30, "Check retrieving maxLength" );
485 jQuery("#table").prop("cellspacing", 1);
486 equals( jQuery("#table").prop("cellSpacing"), "1", "Check setting and retrieving cellSpacing" );
487 jQuery("#table").prop("cellpadding", 1);
488 equals( jQuery("#table").prop("cellPadding"), "1", "Check setting and retrieving cellPadding" );
489 jQuery("#table").prop("rowspan", 1);
490 equals( jQuery("#table").prop("rowSpan"), 1, "Check setting and retrieving rowSpan" );
491 jQuery("#table").prop("colspan", 1);
492 equals( jQuery("#table").prop("colSpan"), 1, "Check setting and retrieving colSpan" );
493 jQuery("#table").prop("usemap", 1);
494 equals( jQuery("#table").prop("useMap"), 1, "Check setting and retrieving useMap" );
495 jQuery("#table").prop("frameborder", 1);
496 equals( jQuery("#table").prop("frameBorder"), 1, "Check setting and retrieving frameBorder" );
499 var body = document.body, $body = jQuery( body );
500 ok( $body.prop("nextSibling") === null, "Make sure a null expando returns null" );
502 equals( $body.prop("foo"), "bar", "Make sure the expando is preferred over the dom attribute" );
503 body.foo = undefined;
504 ok( $body.prop("foo") === undefined, "Make sure the expando is preferred over the dom attribute, even if undefined" );
506 var select = document.createElement("select"), optgroup = document.createElement("optgroup"), option = document.createElement("option");
507 optgroup.appendChild( option );
508 select.appendChild( optgroup );
510 equals( jQuery(option).prop("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
511 equals( jQuery(document).prop("nodeName"), "#document", "prop works correctly on document nodes (bug #7451)." );
513 var attributeNode = document.createAttribute("irrelevant"),
514 commentNode = document.createComment("some comment"),
515 textNode = document.createTextNode("some text"),
517 jQuery.each( [document, attributeNode, commentNode, textNode, obj, "#firstp"], function( i, ele ) {
518 strictEqual( jQuery(ele).prop("nonexisting"), undefined, "prop works correctly for non existing attributes (bug #7500)." );
522 jQuery.each( [document, obj], function( i, ele ) {
523 var $ele = jQuery( ele );
524 $ele.prop( "nonexisting", "foo" );
525 equal( $ele.prop("nonexisting"), "foo", "prop(name, value) works correctly for non existing attributes (bug #7500)." );
527 jQuery( document ).removeProp("nonexisting");
530 test("prop('tabindex')", function() {
533 // elements not natively tabbable
534 equals(jQuery("#listWithTabIndex").prop("tabindex"), 5, "not natively tabbable, with tabindex set to 0");
535 equals(jQuery("#divWithNoTabIndex").prop("tabindex"), undefined, "not natively tabbable, no tabindex set");
538 equals(jQuery("#linkWithNoTabIndex").prop("tabindex"), 0, "anchor with href, no tabindex set");
539 equals(jQuery("#linkWithTabIndex").prop("tabindex"), 2, "anchor with href, tabindex set to 2");
540 equals(jQuery("#linkWithNegativeTabIndex").prop("tabindex"), -1, "anchor with href, tabindex set to -1");
542 // anchor without href
543 equals(jQuery("#linkWithNoHrefWithNoTabIndex").prop("tabindex"), undefined, "anchor without href, no tabindex set");
544 equals(jQuery("#linkWithNoHrefWithTabIndex").prop("tabindex"), 1, "anchor without href, tabindex set to 2");
545 equals(jQuery("#linkWithNoHrefWithNegativeTabIndex").prop("tabindex"), -1, "anchor without href, no tabindex set");
548 test("prop('tabindex', value)", function() {
551 var element = jQuery("#divWithNoTabIndex");
552 equals(element.prop("tabindex"), undefined, "start with no tabindex");
554 // set a positive string
555 element.prop("tabindex", "1");
556 equals(element.prop("tabindex"), 1, "set tabindex to 1 (string)");
559 element.prop("tabindex", "0");
560 equals(element.prop("tabindex"), 0, "set tabindex to 0 (string)");
562 // set a negative string
563 element.prop("tabindex", "-1");
564 equals(element.prop("tabindex"), -1, "set tabindex to -1 (string)");
566 // set a positive number
567 element.prop("tabindex", 1);
568 equals(element.prop("tabindex"), 1, "set tabindex to 1 (number)");
571 element.prop("tabindex", 0);
572 equals(element.prop("tabindex"), 0, "set tabindex to 0 (number)");
574 // set a negative number
575 element.prop("tabindex", -1);
576 equals(element.prop("tabindex"), -1, "set tabindex to -1 (number)");
578 element = jQuery("#linkWithTabIndex");
579 equals(element.prop("tabindex"), 2, "start with tabindex 2");
581 element.prop("tabindex", -1);
582 equals(element.prop("tabindex"), -1, "set negative tabindex");
585 test("removeProp(String)", function() {
587 var attributeNode = document.createAttribute("irrelevant"),
588 commentNode = document.createComment("some comment"),
589 textNode = document.createTextNode("some text"),
592 strictEqual( jQuery( "#firstp" ).prop( "nonexisting", "foo" ).removeProp( "nonexisting" )[0].nonexisting, undefined, "removeprop works correctly on DOM element nodes" );
594 jQuery.each( [document, obj], function( i, ele ) {
595 var $ele = jQuery( ele );
596 $ele.prop( "nonexisting", "foo" ).removeProp( "nonexisting" );
597 strictEqual( ele.nonexisting, undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
599 jQuery.each( [commentNode, textNode, attributeNode], function( i, ele ) {
600 var $ele = jQuery( ele );
601 $ele.prop( "nonexisting", "foo" ).removeProp( "nonexisting" );
602 strictEqual( ele.nonexisting, undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
606 test("val()", function() {
609 document.getElementById("text1").value = "bla";
610 equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
614 equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
615 // ticket #1714 this caused a JS error in IE
616 equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
617 ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
619 equals( jQuery("#select2").val(), "3", "Call val() on a single=\"single\" select" );
621 same( jQuery("#select3").val(), ["1", "2"], "Call val() on a multiple=\"multiple\" select" );
623 equals( jQuery("#option3c").val(), "2", "Call val() on a option element with value" );
625 equals( jQuery("#option3a").val(), "", "Call val() on a option element with empty value" );
627 equals( jQuery("#option3e").val(), "no value", "Call val() on a option element with no value attribute" );
629 equals( jQuery("#option3a").val(), "", "Call val() on a option element with no value attribute" );
631 jQuery("#select3").val("");
632 same( jQuery("#select3").val(), [""], "Call val() on a multiple=\"multiple\" select" );
634 same( jQuery("#select4").val(), [], "Call val() on multiple=\"multiple\" select with all disabled options" );
636 jQuery("#select4 optgroup").add("#select4 > [disabled]").attr("disabled", false);
637 same( jQuery("#select4").val(), ["2", "3"], "Call val() on multiple=\"multiple\" select with some disabled options" );
639 jQuery("#select4").attr("disabled", true);
640 same( jQuery("#select4").val(), ["2", "3"], "Call val() on disabled multiple=\"multiple\" select" );
642 equals( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
644 jQuery("#select5").val(1);
645 equals( jQuery("#select5").val(), "1", "Check value on ambiguous select." );
647 jQuery("#select5").val(3);
648 equals( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
650 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");
652 same( checks.serialize(), "", "Get unchecked values." );
654 equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
657 same( checks.serialize(), "test=2", "Get a single checked value." );
659 checks.val([ "1", "" ]);
660 same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
662 checks.val([ "", "2" ]);
663 same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
665 checks.val([ "1", "on" ]);
666 same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
670 var $button = jQuery("<button value='foobar'>text</button>").insertAfter("#button");
671 equals( $button.val(), "foobar", "Value retrieval on a button does not return innerHTML" );
672 equals( $button.val("baz").html(), "text", "Setting the value does not change innerHTML" );
674 equals( jQuery("<option/>").val("test").attr("value"), "test", "Setting value sets the value attribute" );
677 if ( "value" in document.createElement("meter") &&
678 "value" in document.createElement("progress") ) {
680 test("val() respects numbers without exception (Bug #9319)", function() {
684 var $meter = jQuery("<meter min='0' max='10' value='5.6'></meter>"),
685 $progress = jQuery("<progress max='10' value='1.5'></progress>");
688 equal( typeof $meter.val(), "number", "meter, returns a number and does not throw exception" );
689 equal( $meter.val(), $meter[0].value, "meter, api matches host and does not throw exception" );
691 equal( typeof $progress.val(), "number", "progress, returns a number and does not throw exception" );
692 equal( $progress.val(), $progress[0].value, "progress, api matches host and does not throw exception" );
701 var testVal = function(valueObj) {
705 jQuery("#text1").val(valueObj( "test" ));
706 equals( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" );
708 jQuery("#text1").val(valueObj( undefined ));
709 equals( document.getElementById("text1").value, "", "Check for modified (via val(undefined)) value of input element" );
711 jQuery("#text1").val(valueObj( 67 ));
712 equals( document.getElementById("text1").value, "67", "Check for modified (via val(Number)) value of input element" );
714 jQuery("#text1").val(valueObj( null ));
715 equals( document.getElementById("text1").value, "", "Check for modified (via val(null)) value of input element" );
717 var $select1 = jQuery("#select1");
718 $select1.val(valueObj( "3" ));
719 equals( $select1.val(), "3", "Check for modified (via val(String)) value of select element" );
721 $select1.val(valueObj( 2 ));
722 equals( $select1.val(), "2", "Check for modified (via val(Number)) value of select element" );
724 $select1.append("<option value='4'>four</option>");
725 $select1.val(valueObj( 4 ));
726 equals( $select1.val(), "4", "Should be possible to set the val() to a newly created option" );
728 // using contents will get comments regular, text, and comment nodes
729 var j = jQuery("#nonnodes").contents();
730 j.val(valueObj( "asdf" ));
731 equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
732 j.removeAttr("value");
735 test("val(String/Number)", function() {
739 test("val(Function)", function() {
740 testVal(functionReturningObj);
743 test( "val(Array of Numbers) (Bug #7123)", function() {
745 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' />");
746 var elements = jQuery("input[name=arrayTest]").val([ 1, 2 ]);
747 ok( elements[0].checked, "First element was checked" );
748 ok( elements[1].checked, "Second element was checked" );
749 ok( !elements[2].checked, "Third element was unchecked" );
750 ok( !elements[3].checked, "Fourth element remained unchecked" );
755 test("val(Function) with incoming value", function() {
759 var oldVal = jQuery("#text1").val();
761 jQuery("#text1").val(function(i, val) {
762 equals( val, oldVal, "Make sure the incoming value is correct." );
766 equals( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" );
768 oldVal = jQuery("#text1").val();
770 jQuery("#text1").val(function(i, val) {
771 equals( val, oldVal, "Make sure the incoming value is correct." );
775 equals( document.getElementById("text1").value, "67", "Check for modified (via val(Number)) value of input element" );
777 oldVal = jQuery("#select1").val();
779 jQuery("#select1").val(function(i, val) {
780 equals( val, oldVal, "Make sure the incoming value is correct." );
784 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
786 oldVal = jQuery("#select1").val();
788 jQuery("#select1").val(function(i, val) {
789 equals( val, oldVal, "Make sure the incoming value is correct." );
793 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
795 jQuery("#select1").append("<option value='4'>four</option>");
797 oldVal = jQuery("#select1").val();
799 jQuery("#select1").val(function(i, val) {
800 equals( val, oldVal, "Make sure the incoming value is correct." );
804 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
807 // testing if a form.reset() breaks a subsequent call to a select element's .val() (in IE only)
808 test("val(select) after form.reset() (Bug #2551)", function() {
811 jQuery("<form id='kk' name='kk'><select id='kkk'><option value='cf'>cf</option><option value='gf'>gf</option></select></form>").appendTo("#qunit-fixture");
813 jQuery("#kkk").val( "gf" );
817 equal( jQuery("#kkk")[0].value, "cf", "Check value of select after form reset." );
818 equal( jQuery("#kkk").val(), "cf", "Check value of select after form reset." );
820 // re-verify the multi-select is not broken (after form.reset) by our fix for single-select
821 same( jQuery("#select3").val(), ["1", "2"], "Call val() on a multiple=\"multiple\" select" );
823 jQuery("#kk").remove();
826 var testAddClass = function(valueObj) {
829 var div = jQuery("div");
830 div.addClass( valueObj("test") );
832 for ( var i = 0; i < div.size(); i++ ) {
833 if ( !~div.get(i).className.indexOf("test") ) {
837 ok( pass, "Add Class" );
839 // using contents will get regular, text, and comment nodes
840 var j = jQuery("#nonnodes").contents();
841 j.addClass( valueObj("asdf") );
842 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
844 div = jQuery("<div/>");
846 div.addClass( valueObj("test") );
847 equals( div.attr("class"), "test", "Make sure there's no extra whitespace." );
849 div.attr("class", " foo");
850 div.addClass( valueObj("test") );
851 equals( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
853 div.attr("class", "foo");
854 div.addClass( valueObj("bar baz") );
855 equals( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
858 div.addClass( valueObj("foo") ).addClass( valueObj("foo") )
859 equal( div.attr("class"), "foo", "Do not add the same class twice in separate calls." );
861 div.addClass( valueObj("fo") );
862 equal( div.attr("class"), "foo fo", "Adding a similar class does not get interrupted." );
863 div.removeClass().addClass("wrap2");
864 ok( div.addClass("wrap").hasClass("wrap"), "Can add similarly named classes");
867 div.addClass( valueObj("bar bar") );
868 equal( div.attr("class"), "bar", "Do not add the same class twice in the same call." );
871 test("addClass(String)", function() {
872 testAddClass(bareObj);
875 test("addClass(Function)", function() {
876 testAddClass(functionReturningObj);
879 test("addClass(Function) with incoming value", function() {
881 var div = jQuery("div"), old = div.map(function(){
882 return jQuery(this).attr("class") || "";
885 div.addClass(function(i, val) {
886 if ( this.id !== "_firebugConsole") {
887 equals( val, old[i], "Make sure the incoming value is correct." );
893 for ( var i = 0; i < div.length; i++ ) {
894 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
896 ok( pass, "Add Class" );
899 var testRemoveClass = function(valueObj) {
902 var $divs = jQuery("div");
904 $divs.addClass("test").removeClass( valueObj("test") );
906 ok( !$divs.is(".test"), "Remove Class" );
909 $divs = jQuery("div");
911 $divs.addClass("test").addClass("foo").addClass("bar");
912 $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
914 ok( !$divs.is(".test,.bar,.foo"), "Remove multiple classes" );
917 $divs = jQuery("div");
919 // Make sure that a null value doesn't cause problems
920 $divs.eq(0).addClass("test").removeClass( valueObj(null) );
921 ok( $divs.eq(0).is(".test"), "Null value passed to removeClass" );
923 $divs.eq(0).addClass("test").removeClass( valueObj("") );
924 ok( $divs.eq(0).is(".test"), "Empty string passed to removeClass" );
926 // using contents will get regular, text, and comment nodes
927 var j = jQuery("#nonnodes").contents();
928 j.removeClass( valueObj("asdf") );
929 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
931 var div = document.createElement("div");
932 div.className = " test foo ";
934 jQuery(div).removeClass( valueObj("foo") );
935 equals( div.className, "test", "Make sure remaining className is trimmed." );
937 div.className = " test ";
939 jQuery(div).removeClass( valueObj("test") );
940 equals( div.className, "", "Make sure there is nothing left after everything is removed." );
943 test("removeClass(String) - simple", function() {
944 testRemoveClass(bareObj);
947 test("removeClass(Function) - simple", function() {
948 testRemoveClass(functionReturningObj);
951 test("removeClass(Function) with incoming value", function() {
954 var $divs = jQuery("div").addClass("test"), old = $divs.map(function(){
955 return jQuery(this).attr("class");
958 $divs.removeClass(function(i, val) {
959 if ( this.id !== "_firebugConsole" ) {
960 equals( val, old[i], "Make sure the incoming value is correct." );
965 ok( !$divs.is(".test"), "Remove Class" );
970 var testToggleClass = function(valueObj) {
973 var e = jQuery("#firstp");
974 ok( !e.is(".test"), "Assert class not present" );
975 e.toggleClass( valueObj("test") );
976 ok( e.is(".test"), "Assert class present" );
977 e.toggleClass( valueObj("test") );
978 ok( !e.is(".test"), "Assert class not present" );
980 // class name with a boolean
981 e.toggleClass( valueObj("test"), false );
982 ok( !e.is(".test"), "Assert class not present" );
983 e.toggleClass( valueObj("test"), true );
984 ok( e.is(".test"), "Assert class present" );
985 e.toggleClass( valueObj("test"), false );
986 ok( !e.is(".test"), "Assert class not present" );
988 // multiple class names
989 e.addClass("testA testB");
990 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
991 e.toggleClass( valueObj("testB testC") );
992 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
993 e.toggleClass( valueObj("testA testC") );
994 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
996 // toggleClass storage
998 ok( e[0].className === "", "Assert class is empty (data was empty)" );
999 e.addClass("testD testE");
1000 ok( e.is(".testD.testE"), "Assert class present" );
1002 ok( !e.is(".testD.testE"), "Assert class not present" );
1003 ok( jQuery._data(e[0], "__className__") === "testD testE", "Assert data was stored" );
1005 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
1006 e.toggleClass(false);
1007 ok( !e.is(".testD.testE"), "Assert class not present" );
1008 e.toggleClass(true);
1009 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
1011 e.toggleClass(false);
1013 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
1016 e.removeClass("testD");
1017 jQuery.removeData(e[0], "__className__", true);
1020 test("toggleClass(String|boolean|undefined[, boolean])", function() {
1021 testToggleClass(bareObj);
1024 test("toggleClass(Function[, boolean])", function() {
1025 testToggleClass(functionReturningObj);
1028 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
1031 var e = jQuery("#firstp"), old = e.attr("class") || "";
1032 ok( !e.is(".test"), "Assert class not present" );
1034 e.toggleClass(function(i, val) {
1035 equal( old, val, "Make sure the incoming value is correct." );
1038 ok( e.is(".test"), "Assert class present" );
1040 old = e.attr("class");
1042 e.toggleClass(function(i, val) {
1043 equal( old, val, "Make sure the incoming value is correct." );
1046 ok( !e.is(".test"), "Assert class not present" );
1048 old = e.attr("class") || "";
1050 // class name with a boolean
1051 e.toggleClass(function(i, val, state) {
1052 equal( old, val, "Make sure the incoming value is correct." );
1053 equal( state, false, "Make sure that the state is passed in." );
1056 ok( !e.is(".test"), "Assert class not present" );
1058 old = e.attr("class") || "";
1060 e.toggleClass(function(i, val, state) {
1061 equal( old, val, "Make sure the incoming value is correct." );
1062 equal( state, true, "Make sure that the state is passed in." );
1065 ok( e.is(".test"), "Assert class present" );
1067 old = e.attr("class");
1069 e.toggleClass(function(i, val, state) {
1070 equal( old, val, "Make sure the incoming value is correct." );
1071 equal( state, false, "Make sure that the state is passed in." );
1074 ok( !e.is(".test"), "Assert class not present" );
1077 e.removeClass("test");
1078 jQuery.removeData(e[0], "__className__", true);
1081 test("addClass, removeClass, hasClass", function() {
1084 var jq = jQuery("<p>Hi</p>"), x = jq[0];
1087 equals( x.className, "hi", "Check single added class" );
1089 jq.addClass("foo bar");
1090 equals( x.className, "hi foo bar", "Check more added classes" );
1093 equals( x.className, "", "Remove all classes" );
1095 jq.addClass("hi foo bar");
1096 jq.removeClass("foo");
1097 equals( x.className, "hi bar", "Check removal of one class" );
1099 ok( jq.hasClass("hi"), "Check has1" );
1100 ok( jq.hasClass("bar"), "Check has2" );
1102 var jq = jQuery("<p class='class1\nclass2\tcla.ss3\n\rclass4'></p>");
1103 ok( jq.hasClass("class1"), "Check hasClass with line feed" );
1104 ok( jq.is(".class1"), "Check is with line feed" );
1105 ok( jq.hasClass("class2"), "Check hasClass with tab" );
1106 ok( jq.is(".class2"), "Check is with tab" );
1107 ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
1108 ok( jq.hasClass("class4"), "Check hasClass with carriage return" );
1109 ok( jq.is(".class4"), "Check is with carriage return" );
1111 jq.removeClass("class2");
1112 ok( jq.hasClass("class2")==false, "Check the class has been properly removed" );
1113 jq.removeClass("cla");
1114 ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
1115 jq.removeClass("cla.ss3");
1116 ok( jq.hasClass("cla.ss3")==false, "Check the dotted class has been removed" );
1117 jq.removeClass("class4");
1118 ok( jq.hasClass("class4")==false, "Check the class has been properly removed" );
1121 test("contents().hasClass() returns correct values", function() {
1124 var $div = jQuery("<div><span class='foo'></span><!-- comment -->text</div>"),
1125 $contents = $div.contents();
1127 ok( $contents.hasClass("foo"), "Found 'foo' in $contents" );
1128 ok( !$contents.hasClass("undefined"), "Did not find 'undefined' in $contents (correctly)" );