1 test("attr(String)", function() {
3 equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
4 equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
5 equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
6 equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );
7 equals( jQuery('#check1').attr('type'), "checkbox", 'Check for type attribute' );
8 equals( jQuery('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );
9 equals( jQuery('#google').attr('title'), "Google!", 'Check for title attribute' );
10 equals( jQuery('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );
11 equals( jQuery('#en').attr('lang'), "en", 'Check for lang attribute' );
12 equals( jQuery('#simon').attr('class'), "blog link", 'Check for class attribute' );
13 equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );
14 equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );
15 ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
16 equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
17 equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
18 equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
19 equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
20 equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
21 equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
23 jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
24 equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
26 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
29 // Related to [5574] and [5683]
30 var body = document.body, $body = jQuery(body);
32 ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
33 ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
35 body.setAttribute('foo', 'baz');
36 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
39 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
41 $body.attr('foo','cool');
42 equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
45 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
47 body.removeAttribute('foo'); // Cleanup
51 test("attr(String) in XML Files", function() {
54 jQuery.get("data/dashboard.xml", function(xml) {
55 equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
56 equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
62 test("attr(String, Function)", function() {
64 equals( jQuery('#text1').attr('value', function() { return this.id })[0].value, "text1", "Set value from id" );
65 equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
68 test("attr(Hash)", function() {
71 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
72 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
74 ok( pass, "Set Multiple Attributes" );
77 test("attr(String, Object)", function() {
79 var div = jQuery("div").attr("foo", "bar"),
81 for ( var i = 0; i < div.size(); i++ ) {
82 if ( div.get(i).getAttribute('foo') != "bar" ){
87 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
89 ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
91 jQuery("#name").attr('name', 'something');
92 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
93 jQuery("#check2").attr('checked', true);
94 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
95 jQuery("#check2").attr('checked', false);
96 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
97 jQuery("#text1").attr('readonly', true);
98 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
99 jQuery("#text1").attr('readonly', false);
100 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
101 jQuery("#name").attr('maxlength', '5');
102 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
103 jQuery("#name").attr('maxLength', '10');
104 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
107 jQuery("#name").attr('someAttr', '0');
108 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
109 jQuery("#name").attr('someAttr', 0);
110 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
111 jQuery("#name").attr('someAttr', 1);
112 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
114 // using contents will get comments regular, text, and comment nodes
115 var j = jQuery("#nonnodes").contents();
117 j.attr("name", "attrvalue");
118 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
119 j.removeAttr("name");
123 var type = jQuery("#check2").attr('type');
126 jQuery("#check2").attr('type','hidden');
130 ok( thrown, "Exception thrown when trying to change type property" );
131 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
133 var check = document.createElement("input");
136 jQuery(check).attr('type','checkbox');
140 ok( thrown, "Exception thrown when trying to change type property" );
141 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
143 var check = jQuery("<input />");
146 check.attr('type','checkbox');
150 ok( thrown, "Exception thrown when trying to change type property" );
151 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
153 var button = jQuery("#button");
156 button.attr('type','submit');
160 ok( thrown, "Exception thrown when trying to change type property" );
161 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
165 test("attr(String, Object) - Loaded via XML document", function() {
168 jQuery.get('data/dashboard.xml', function(xml) {
170 jQuery('tab', xml).each(function() {
171 titles.push(jQuery(this).attr('title'));
173 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
174 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
180 test("attr('tabindex')", function() {
183 // elements not natively tabbable
184 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
185 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
188 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
189 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
190 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
192 // anchor without href
193 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
194 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
195 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
198 test("attr('tabindex', value)", function() {
201 var element = jQuery('#divWithNoTabIndex');
202 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
204 // set a positive string
205 element.attr('tabindex', '1');
206 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
209 element.attr('tabindex', '0');
210 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
212 // set a negative string
213 element.attr('tabindex', '-1');
214 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
216 // set a positive number
217 element.attr('tabindex', 1);
218 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
221 element.attr('tabindex', 0);
222 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
224 // set a negative number
225 element.attr('tabindex', -1);
226 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
228 element = jQuery('#linkWithTabIndex');
229 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
231 element.attr('tabindex', -1);
232 equals(element.attr('tabindex'), -1, 'set negative tabindex');
235 test("css(String|Hash)", function() {
238 equals( jQuery('#main').css("display"), 'none', 'Check for css property "display"');
240 ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
241 jQuery('#nothiddendiv').css({display: 'none'});
242 ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
243 jQuery('#nothiddendiv').css({display: 'block'});
244 ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
246 jQuery('#floatTest').css({styleFloat: 'right'});
247 equals( jQuery('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right');
248 jQuery('#floatTest').css({cssFloat: 'left'});
249 equals( jQuery('#floatTest').css('cssFloat'), 'left', 'Modified CSS float using "cssFloat": Assert float is left');
250 jQuery('#floatTest').css({'float': 'right'});
251 equals( jQuery('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right');
252 jQuery('#floatTest').css({'font-size': '30px'});
253 equals( jQuery('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px');
255 jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
256 jQuery('#foo').css({opacity: n});
257 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
258 jQuery('#foo').css({opacity: parseFloat(n)});
259 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
261 jQuery('#foo').css({opacity: ''});
262 equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
265 test("css(String, Object)", function() {
267 ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
268 jQuery('#nothiddendiv').css("display", 'none');
269 ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
270 jQuery('#nothiddendiv').css("display", 'block');
271 ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
273 jQuery('#floatTest').css('styleFloat', 'left');
274 equals( jQuery('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left');
275 jQuery('#floatTest').css('cssFloat', 'right');
276 equals( jQuery('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right');
277 jQuery('#floatTest').css('float', 'left');
278 equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
279 jQuery('#floatTest').css('font-size', '20px');
280 equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
282 jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
283 jQuery('#foo').css('opacity', n);
284 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
285 jQuery('#foo').css('opacity', parseFloat(n));
286 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
288 jQuery('#foo').css('opacity', '');
289 equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
290 // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
291 if (jQuery.browser.msie) {
292 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
294 equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
296 // using contents will get comments regular, text, and comment nodes
297 var j = jQuery("#nonnodes").contents();
298 j.css("padding-left", "1px");
299 equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
301 // opera sometimes doesn't update 'display' correctly, see #2037
302 jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML
303 equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
306 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
309 var $checkedtest = jQuery("#checkedtest");
310 // IE6 was clearing "checked" in jQuery.css(elem, "height");
311 jQuery.css($checkedtest[0], "height");
312 ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
313 ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
314 ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
315 ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
318 test("width()", function() {
321 var $div = jQuery("#nothiddendiv");
323 equals($div.width(), 30, "Test set to 30 correctly");
325 equals($div.width(), 30, "Test hidden div");
327 $div.width(-1); // handle negative numbers by ignoring #1599
328 equals($div.width(), 30, "Test negative width ignored");
329 $div.css("padding", "20px");
330 equals($div.width(), 30, "Test padding specified with pixels");
331 $div.css("border", "2px solid #fff");
332 equals($div.width(), 30, "Test border specified with pixels");
333 //$div.css("padding", "2em");
334 //equals($div.width(), 30, "Test padding specified with ems");
335 //$div.css("border", "1em solid #fff");
336 //DISABLED - Opera 9.6 fails this test, returns 8
337 //equals($div.width(), 30, "Test border specified with ems");
338 //$div.css("padding", "2%");
339 //equals($div.width(), 30, "Test padding specified with percent");
341 $div.css({ display: "", border: "", padding: "" });
343 jQuery("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" });
344 equals(jQuery("#nothiddendivchild").width(), 20, "Test child width with border and padding");
345 jQuery("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" });
348 test("height()", function() {
351 var $div = jQuery("#nothiddendiv");
353 equals($div.height(), 30, "Test set to 30 correctly");
355 equals($div.height(), 30, "Test hidden div");
357 $div.height(-1); // handle negative numbers by ignoring #1599
358 equals($div.height(), 30, "Test negative height ignored");
359 $div.css("padding", "20px");
360 equals($div.height(), 30, "Test padding specified with pixels");
361 $div.css("border", "2px solid #fff");
362 equals($div.height(), 30, "Test border specified with pixels");
363 //$div.css("padding", "2em");
364 //equals($div.height(), 30, "Test padding specified with ems");
365 //$div.css("border", "1em solid #fff");
366 //DISABLED - Opera 9.6 fails this test, returns 8
367 //equals($div.height(), 30, "Test border specified with ems");
368 //$div.css("padding", "2%");
369 //equals($div.height(), 30, "Test padding specified with percent");
371 $div.css({ display: "", border: "", padding: "", height: "1px" });
374 test("addClass(String)", function() {
376 var div = jQuery("div");
377 div.addClass("test");
379 for ( var i = 0; i < div.size(); i++ ) {
380 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
382 ok( pass, "Add Class" );
384 // using contents will get regular, text, and comment nodes
385 var j = jQuery("#nonnodes").contents();
387 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
390 test("removeClass(String) - simple", function() {
393 var $divs = jQuery('div');
395 $divs.addClass("test").removeClass("test");
397 ok( !$divs.is('.test'), "Remove Class" );
401 $divs.addClass("test").addClass("foo").addClass("bar");
402 $divs.removeClass("test").removeClass("bar").removeClass("foo");
404 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
408 // Make sure that a null value doesn't cause problems
409 $divs.eq(0).addClass("test").removeClass(null);
410 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
412 $divs.eq(0).addClass("test").removeClass("");
413 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
415 // using contents will get regular, text, and comment nodes
416 var j = jQuery("#nonnodes").contents();
417 j.removeClass("asdf");
418 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
421 test("toggleClass(String)", function() {
423 var e = jQuery("#firstp");
424 ok( !e.is(".test"), "Assert class not present" );
425 e.toggleClass("test");
426 ok( e.is(".test"), "Assert class present" );
427 e.toggleClass("test");
428 ok( !e.is(".test"), "Assert class not present" );
430 e.toggleClass("test", false);
431 ok( !e.is(".test"), "Assert class not present" );
432 e.toggleClass("test", true);
433 ok( e.is(".test"), "Assert class present" );
434 e.toggleClass("test", false);
435 ok( !e.is(".test"), "Assert class not present" );
438 test("removeAttr(String", function() {
440 equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
443 test("jQuery.className", function() {
445 var x = jQuery("<p>Hi</p>")[0];
446 var c = jQuery.className;
448 equals( x.className, "hi", "Check single added class" );
450 equals( x.className, "hi foo bar", "Check more added classes" );
452 equals( x.className, "", "Remove all classes" );
453 c.add(x, "hi foo bar");
455 equals( x.className, "hi bar", "Check removal of one class" );
456 ok( c.has(x, "hi"), "Check has1" );
457 ok( c.has(x, "bar"), "Check has2" );