3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
6 test("attr(String)", function() {
9 // This one sometimes fails randomly ?!
10 equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
12 equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
13 equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
14 equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );
15 equals( jQuery('#check1').attr('type'), "checkbox", 'Check for type attribute' );
16 equals( jQuery('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );
17 equals( jQuery('#google').attr('title'), "Google!", 'Check for title attribute' );
18 equals( jQuery('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );
19 equals( jQuery('#en').attr('lang'), "en", 'Check for lang attribute' );
20 equals( jQuery('#simon').attr('class'), "blog link", 'Check for class attribute' );
21 equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );
22 equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );
23 ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
24 // Temporarily disabled. See: #4299
25 // ok( jQuery('#form').attr('action','newformaction').attr('action').indexOf("newformaction") >= 0, 'Check that action attribute was changed' );
26 equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
27 equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
28 equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
29 equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
30 equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
31 equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
33 jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
34 equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
36 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
39 // Related to [5574] and [5683]
40 var body = document.body, $body = jQuery(body);
42 ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
43 ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
45 body.setAttribute('foo', 'baz');
46 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
49 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
51 $body.attr('foo','cool');
52 equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
55 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
57 body.removeAttribute('foo'); // Cleanup
59 var select = document.createElement("select"), optgroup = document.createElement("optgroup"), option = document.createElement("option");
60 optgroup.appendChild( option );
61 select.appendChild( optgroup );
63 equals( jQuery(option).attr("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
67 test("attr(String) in XML Files", function() {
70 jQuery.get("data/dashboard.xml", function(xml) {
71 equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
72 equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
78 test("attr(String, Function)", function() {
80 equals( jQuery('#text1').attr('value', function() { return this.id ;})[0].value, "text1", "Set value from id" );
81 equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
84 test("attr(Hash)", function() {
87 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
88 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
90 ok( pass, "Set Multiple Attributes" );
91 equals( jQuery('#text1').attr({'value': function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
92 equals( jQuery('#text1').attr({'title': function(i) { return i; }}).attr('title'), "0", "Set attribute to computed value #2");
96 test("attr(String, Object)", function() {
98 var div = jQuery("div").attr("foo", "bar"),
100 for ( var i = 0; i < div.size(); i++ ) {
101 if ( div.get(i).getAttribute('foo') != "bar" ){
106 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
108 // Fails on IE since recent changes to .attr()
109 // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
111 jQuery("#name").attr('name', 'something');
112 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
113 jQuery("#check2").attr('checked', true);
114 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
115 jQuery("#check2").attr('checked', false);
116 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
117 jQuery("#text1").attr('readonly', true);
118 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
119 jQuery("#text1").attr('readonly', false);
120 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
121 jQuery("#name").attr('maxlength', '5');
122 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
123 jQuery("#name").attr('maxLength', '10');
124 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
126 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>"),
127 td = table.find('td:first');
128 td.attr("rowspan", "2");
129 equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
130 td.attr("colspan", "2");
131 equals( td[0].colSpan, 2, "Check colspan is correctly set" );
132 table.attr("cellspacing", "2");
133 equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
136 jQuery("#name").attr('someAttr', '0');
137 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
138 jQuery("#name").attr('someAttr', 0);
139 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
140 jQuery("#name").attr('someAttr', 1);
141 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
143 // using contents will get comments regular, text, and comment nodes
144 var j = jQuery("#nonnodes").contents();
146 j.attr("name", "attrvalue");
147 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
148 j.removeAttr("name");
152 var type = jQuery("#check2").attr('type');
155 jQuery("#check2").attr('type','hidden');
159 ok( thrown, "Exception thrown when trying to change type property" );
160 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
162 var check = document.createElement("input");
165 jQuery(check).attr('type','checkbox');
169 ok( thrown, "Exception thrown when trying to change type property" );
170 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
172 var check = jQuery("<input />");
175 check.attr('type','checkbox');
179 ok( thrown, "Exception thrown when trying to change type property" );
180 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
182 var button = jQuery("#button");
185 button.attr('type','submit');
189 ok( thrown, "Exception thrown when trying to change type property" );
190 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
193 test("attr(jquery_method)", function(){
196 var $elem = jQuery("<div />"),
200 $elem.attr({'html': 'foo'}, true);
201 equals( elem.innerHTML, 'foo', 'attr(html)');
203 $elem.attr({'text': 'bar'}, true);
204 equals( elem.innerHTML, 'bar', 'attr(text)');
206 $elem.attr({'css': {color:'red'}}, true);
207 ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
209 $elem.attr({'height': 10}, true);
210 equals( elem.style.height, '10px', 'attr(height)');
212 // Multiple attributes
216 css:{ paddingLeft:1, paddingRight:1 }
219 equals( elem.style.width, '10px', 'attr({...})');
220 equals( elem.style.paddingLeft, '1px', 'attr({...})');
221 equals( elem.style.paddingRight, '1px', 'attr({...})');
225 test("attr(String, Object) - Loaded via XML document", function() {
228 jQuery.get('data/dashboard.xml', function(xml) {
230 jQuery('tab', xml).each(function() {
231 titles.push(jQuery(this).attr('title'));
233 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
234 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
240 test("attr('tabindex')", function() {
243 // elements not natively tabbable
244 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
245 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
248 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
249 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
250 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
252 // anchor without href
253 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
254 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
255 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
258 test("attr('tabindex', value)", function() {
261 var element = jQuery('#divWithNoTabIndex');
262 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
264 // set a positive string
265 element.attr('tabindex', '1');
266 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
269 element.attr('tabindex', '0');
270 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
272 // set a negative string
273 element.attr('tabindex', '-1');
274 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
276 // set a positive number
277 element.attr('tabindex', 1);
278 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
281 element.attr('tabindex', 0);
282 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
284 // set a negative number
285 element.attr('tabindex', -1);
286 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
288 element = jQuery('#linkWithTabIndex');
289 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
291 element.attr('tabindex', -1);
292 equals(element.attr('tabindex'), -1, 'set negative tabindex');
295 test("val()", function() {
298 document.getElementById('text1').value = "bla";
299 equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
303 equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
304 // ticket #1714 this caused a JS error in IE
305 equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
306 ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
308 equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
310 same( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
312 equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
314 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
316 equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
318 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with no value attribute' );
320 jQuery('#select3').val("");
321 same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );
323 var checks = jQuery("<input type='checkbox' name='test' value='1'/>").appendTo("#form")
324 .add( jQuery("<input type='checkbox' name='test' value='2'/>").appendTo("#form") )
325 .add( jQuery("<input type='checkbox' name='test' value=''/>").appendTo("#form") )
326 .add( jQuery("<input type='checkbox' name='test'/>").appendTo("#form") );
328 same( checks.serialize(), "", "Get unchecked values." );
330 equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
333 same( checks.serialize(), "test=2", "Get a single checked value." );
335 checks.val([ "1", "" ]);
336 same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
338 checks.val([ "", "2" ]);
339 same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
341 checks.val([ "1", "on" ]);
342 same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
347 var testVal = function(valueObj) {
350 jQuery("#text1").val(valueObj( 'test' ));
351 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
353 jQuery("#text1").val(valueObj( 67 ));
354 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
356 jQuery("#select1").val(valueObj( "3" ));
357 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
359 jQuery("#select1").val(valueObj( 2 ));
360 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
362 jQuery("#select1").append("<option value='4'>four</option>");
363 jQuery("#select1").val(valueObj( 4 ));
364 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
366 // using contents will get comments regular, text, and comment nodes
367 var j = jQuery("#nonnodes").contents();
368 j.val(valueObj( "asdf" ));
369 equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
370 j.removeAttr("value");
373 test("val(String/Number)", function() {
377 test("val(Function)", function() {
378 testVal(functionReturningObj);
381 var testAddClass = function(valueObj) {
383 var div = jQuery("div");
384 div.addClass( valueObj("test") );
386 for ( var i = 0; i < div.size(); i++ ) {
387 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
389 ok( pass, "Add Class" );
391 // using contents will get regular, text, and comment nodes
392 var j = jQuery("#nonnodes").contents();
393 j.addClass( valueObj("asdf") );
394 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
397 test("addClass(String)", function() {
398 testAddClass(bareObj);
401 test("addClass(Function)", function() {
402 testAddClass(functionReturningObj);
405 var testRemoveClass = function(valueObj) {
408 var $divs = jQuery('div');
410 $divs.addClass("test").removeClass( valueObj("test") );
412 ok( !$divs.is('.test'), "Remove Class" );
415 $divs = jQuery('div');
417 $divs.addClass("test").addClass("foo").addClass("bar");
418 $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
420 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
423 $divs = jQuery('div');
425 // Make sure that a null value doesn't cause problems
426 $divs.eq(0).addClass("test").removeClass( valueObj(null) );
427 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
429 $divs.eq(0).addClass("test").removeClass( valueObj("") );
430 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
432 // using contents will get regular, text, and comment nodes
433 var j = jQuery("#nonnodes").contents();
434 j.removeClass( valueObj("asdf") );
435 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
438 test("removeClass(String) - simple", function() {
439 testRemoveClass(bareObj);
442 test("removeClass(Function) - simple", function() {
443 testRemoveClass(functionReturningObj);
446 var testToggleClass = function(valueObj) {
449 var e = jQuery("#firstp");
450 ok( !e.is(".test"), "Assert class not present" );
451 e.toggleClass( valueObj("test") );
452 ok( e.is(".test"), "Assert class present" );
453 e.toggleClass( valueObj("test") );
454 ok( !e.is(".test"), "Assert class not present" );
456 // class name with a boolean
457 e.toggleClass( valueObj("test"), false );
458 ok( !e.is(".test"), "Assert class not present" );
459 e.toggleClass( valueObj("test"), true );
460 ok( e.is(".test"), "Assert class present" );
461 e.toggleClass( valueObj("test"), false );
462 ok( !e.is(".test"), "Assert class not present" );
464 // multiple class names
465 e.addClass("testA testB");
466 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
467 e.toggleClass( valueObj("testB testC") );
468 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
469 e.toggleClass( valueObj("testA testC") );
470 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
472 // toggleClass storage
474 ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
475 e.addClass("testD testE");
476 ok( e.is(".testD.testE"), "Assert class present" );
478 ok( !e.is(".testD.testE"), "Assert class not present" );
479 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
481 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
482 e.toggleClass(false);
483 ok( !e.is(".testD.testE"), "Assert class not present" );
485 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
487 e.toggleClass(false);
489 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
494 e.removeClass("testD");
495 e.removeData('__className__');
498 test("toggleClass(String|boolean|undefined[, boolean])", function() {
499 testToggleClass(bareObj);
502 test("toggleClass(Function[, boolean])", function() {
503 testToggleClass(functionReturningObj);
506 var testRemoveAttr = function(valueObj) {
508 equals( jQuery('#mark').removeAttr( valueObj("class") )[0].className, "", "remove class" );
511 test("removeAttr(String)", function() {
512 testRemoveAttr(bareObj);
515 test("removeAttr(Function)", function() {
516 testRemoveAttr(functionReturningObj);
519 test("addClass, removeClass, hasClass", function() {
522 var jq = jQuery("<p>Hi</p>"), x = jq[0];
525 equals( x.className, "hi", "Check single added class" );
527 jq.addClass("foo bar");
528 equals( x.className, "hi foo bar", "Check more added classes" );
531 equals( x.className, "", "Remove all classes" );
533 jq.addClass("hi foo bar");
534 jq.removeClass("foo");
535 equals( x.className, "hi bar", "Check removal of one class" );
537 ok( jq.hasClass("hi"), "Check has1" );
538 ok( jq.hasClass("bar"), "Check has2" );
540 var jq = jQuery("<p class='class1\nclass2\tcla.ss3\n'></p>");
541 ok( jq.hasClass("class1"), "Check hasClass with carriage return" );
542 ok( jq.is(".class1"), "Check is with carriage return" );
543 ok( jq.hasClass("class2"), "Check hasClass with tab" );
544 ok( jq.is(".class2"), "Check is with tab" );
545 ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
547 jq.removeClass("class2");
548 ok( jq.hasClass("class2")==false, "Check the class has been properly removed" );
549 jq.removeClass("cla");
550 ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
551 jq.removeClass("cla.ss3");
552 ok( jq.hasClass("cla.ss3")==false, "Check the dotted class has been removed" );