3 test("attr(String)", function() {
6 // This one sometimes fails randomally ?!
7 equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
9 equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
10 equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
11 equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );
12 equals( jQuery('#check1').attr('type'), "checkbox", 'Check for type attribute' );
13 equals( jQuery('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );
14 equals( jQuery('#google').attr('title'), "Google!", 'Check for title attribute' );
15 equals( jQuery('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );
16 equals( jQuery('#en').attr('lang'), "en", 'Check for lang attribute' );
17 equals( jQuery('#simon').attr('class'), "blog link", 'Check for class attribute' );
18 equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );
19 equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );
20 ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
21 equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
22 equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
23 equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
24 equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
25 equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
26 equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
28 jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
29 equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
31 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
34 // Related to [5574] and [5683]
35 var body = document.body, $body = jQuery(body);
37 ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
38 ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
40 body.setAttribute('foo', 'baz');
41 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
44 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
46 $body.attr('foo','cool');
47 equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
50 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
52 body.removeAttribute('foo'); // Cleanup
56 test("attr(String) in XML Files", function() {
59 jQuery.get("data/dashboard.xml", function(xml) {
60 equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
61 equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
67 test("attr(String, Function)", function() {
69 equals( jQuery('#text1').attr('value', function() { return this.id })[0].value, "text1", "Set value from id" );
70 equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
73 test("attr(Hash)", function() {
76 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
77 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
79 ok( pass, "Set Multiple Attributes" );
80 equals( jQuery('#text1').attr({'value': function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
81 equals( jQuery('#text1').attr({'title': function(i) { return i; }}).attr('title'), "0", "Set attribute to computed value #2");
85 test("attr(String, Object)", function() {
87 var div = jQuery("div").attr("foo", "bar"),
89 for ( var i = 0; i < div.size(); i++ ) {
90 if ( div.get(i).getAttribute('foo') != "bar" ){
95 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
97 ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
99 jQuery("#name").attr('name', 'something');
100 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
101 jQuery("#check2").attr('checked', true);
102 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
103 jQuery("#check2").attr('checked', false);
104 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
105 jQuery("#text1").attr('readonly', true);
106 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
107 jQuery("#text1").attr('readonly', false);
108 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
109 jQuery("#name").attr('maxlength', '5');
110 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
111 jQuery("#name").attr('maxLength', '10');
112 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
114 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>"),
115 td = table.find('td:first');
116 td.attr("rowspan", "2");
117 equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
118 td.attr("colspan", "2");
119 equals( td[0].colSpan, 2, "Check colspan is correctly set" );
120 table.attr("cellspacing", "2");
121 equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
124 jQuery("#name").attr('someAttr', '0');
125 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
126 jQuery("#name").attr('someAttr', 0);
127 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
128 jQuery("#name").attr('someAttr', 1);
129 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
131 // using contents will get comments regular, text, and comment nodes
132 var j = jQuery("#nonnodes").contents();
134 j.attr("name", "attrvalue");
135 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
136 j.removeAttr("name");
140 var type = jQuery("#check2").attr('type');
143 jQuery("#check2").attr('type','hidden');
147 ok( thrown, "Exception thrown when trying to change type property" );
148 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
150 var check = document.createElement("input");
153 jQuery(check).attr('type','checkbox');
157 ok( thrown, "Exception thrown when trying to change type property" );
158 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
160 var check = jQuery("<input />");
163 check.attr('type','checkbox');
167 ok( thrown, "Exception thrown when trying to change type property" );
168 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
170 var button = jQuery("#button");
173 button.attr('type','submit');
177 ok( thrown, "Exception thrown when trying to change type property" );
178 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
182 test("attr(String, Object) - Loaded via XML document", function() {
185 jQuery.get('data/dashboard.xml', function(xml) {
187 jQuery('tab', xml).each(function() {
188 titles.push(jQuery(this).attr('title'));
190 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
191 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
197 test("attr('tabindex')", function() {
200 // elements not natively tabbable
201 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
202 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
205 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
206 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
207 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
209 // anchor without href
210 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
211 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
212 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
215 test("attr('tabindex', value)", function() {
218 var element = jQuery('#divWithNoTabIndex');
219 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
221 // set a positive string
222 element.attr('tabindex', '1');
223 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
226 element.attr('tabindex', '0');
227 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
229 // set a negative string
230 element.attr('tabindex', '-1');
231 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
233 // set a positive number
234 element.attr('tabindex', 1);
235 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
238 element.attr('tabindex', 0);
239 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
241 // set a negative number
242 element.attr('tabindex', -1);
243 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
245 element = jQuery('#linkWithTabIndex');
246 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
248 element.attr('tabindex', -1);
249 equals(element.attr('tabindex'), -1, 'set negative tabindex');
252 test("addClass(String)", function() {
254 var div = jQuery("div");
255 div.addClass("test");
257 for ( var i = 0; i < div.size(); i++ ) {
258 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
260 ok( pass, "Add Class" );
262 // using contents will get regular, text, and comment nodes
263 var j = jQuery("#nonnodes").contents();
265 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
268 test("removeClass(String) - simple", function() {
271 var $divs = jQuery('div');
273 $divs.addClass("test").removeClass("test");
275 ok( !$divs.is('.test'), "Remove Class" );
278 $divs = jQuery('div');
280 $divs.addClass("test").addClass("foo").addClass("bar");
281 $divs.removeClass("test").removeClass("bar").removeClass("foo");
283 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
286 $divs = jQuery('div');
288 // Make sure that a null value doesn't cause problems
289 $divs.eq(0).addClass("test").removeClass(null);
290 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
292 $divs.eq(0).addClass("test").removeClass("");
293 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
295 // using contents will get regular, text, and comment nodes
296 var j = jQuery("#nonnodes").contents();
297 j.removeClass("asdf");
298 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
301 test("toggleClass(String|boolean|undefined[, boolean])", function() {
304 var e = jQuery("#firstp");
305 ok( !e.is(".test"), "Assert class not present" );
306 e.toggleClass("test");
307 ok( e.is(".test"), "Assert class present" );
308 e.toggleClass("test");
309 ok( !e.is(".test"), "Assert class not present" );
311 // class name with a boolean
312 e.toggleClass("test", false);
313 ok( !e.is(".test"), "Assert class not present" );
314 e.toggleClass("test", true);
315 ok( e.is(".test"), "Assert class present" );
316 e.toggleClass("test", false);
317 ok( !e.is(".test"), "Assert class not present" );
319 // multiple class names
320 e.addClass("testA testB");
321 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
322 e.toggleClass("testB testC");
323 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
324 e.toggleClass("testA testC");
325 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
327 // toggleClass storage
329 ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
330 e.addClass("testD testE");
331 ok( e.is(".testD.testE"), "Assert class present" );
333 ok( !e.is(".testD.testE"), "Assert class not present" );
334 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
336 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
337 e.toggleClass(false);
338 ok( !e.is(".testD.testE"), "Assert class not present" );
340 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
342 e.toggleClass(false);
344 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
349 e.removeClass("testD");
350 e.removeData('__className__');
353 test("removeAttr(String", function() {
355 equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
358 test("addClass, removeClass, hasClass", function() {
361 var jq = jQuery("<p>Hi</p>"), x = jq[0];
364 equals( x.className, "hi", "Check single added class" );
366 jq.addClass("foo bar");
367 equals( x.className, "hi foo bar", "Check more added classes" );
370 equals( x.className, "", "Remove all classes" );
372 jq.addClass("hi foo bar");
373 jq.removeClass("foo");
374 equals( x.className, "hi bar", "Check removal of one class" );
376 ok( jq.hasClass("hi"), "Check has1" );
377 ok( jq.hasClass("bar"), "Check has2" );