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 ok( jQuery('#form').attr('action','newformaction').attr('action').indexOf("newformaction") >= 0, 'Check that action attribute was changed' );
22 equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
23 equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
24 equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
25 equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
26 equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
27 equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
29 jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
30 equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
32 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
35 // Related to [5574] and [5683]
36 var body = document.body, $body = jQuery(body);
38 ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
39 ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
41 body.setAttribute('foo', 'baz');
42 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
45 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
47 $body.attr('foo','cool');
48 equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
51 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
53 body.removeAttribute('foo'); // Cleanup
57 test("attr(String) in XML Files", function() {
60 jQuery.get("data/dashboard.xml", function(xml) {
61 equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
62 equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
68 test("attr(String, Function)", function() {
70 equals( jQuery('#text1').attr('value', function() { return this.id })[0].value, "text1", "Set value from id" );
71 equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
74 test("attr(Hash)", function() {
77 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
78 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
80 ok( pass, "Set Multiple Attributes" );
81 equals( jQuery('#text1').attr({'value': function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
82 equals( jQuery('#text1').attr({'title': function(i) { return i; }}).attr('title'), "0", "Set attribute to computed value #2");
86 test("attr(String, Object)", function() {
88 var div = jQuery("div").attr("foo", "bar"),
90 for ( var i = 0; i < div.size(); i++ ) {
91 if ( div.get(i).getAttribute('foo') != "bar" ){
96 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
98 // Fails on IE since recent changes to .attr()
99 // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
101 jQuery("#name").attr('name', 'something');
102 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
103 jQuery("#check2").attr('checked', true);
104 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
105 jQuery("#check2").attr('checked', false);
106 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
107 jQuery("#text1").attr('readonly', true);
108 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
109 jQuery("#text1").attr('readonly', false);
110 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
111 jQuery("#name").attr('maxlength', '5');
112 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
113 jQuery("#name").attr('maxLength', '10');
114 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
116 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>"),
117 td = table.find('td:first');
118 td.attr("rowspan", "2");
119 equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
120 td.attr("colspan", "2");
121 equals( td[0].colSpan, 2, "Check colspan is correctly set" );
122 table.attr("cellspacing", "2");
123 equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
126 jQuery("#name").attr('someAttr', '0');
127 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
128 jQuery("#name").attr('someAttr', 0);
129 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
130 jQuery("#name").attr('someAttr', 1);
131 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
133 // using contents will get comments regular, text, and comment nodes
134 var j = jQuery("#nonnodes").contents();
136 j.attr("name", "attrvalue");
137 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
138 j.removeAttr("name");
142 var type = jQuery("#check2").attr('type');
145 jQuery("#check2").attr('type','hidden');
149 ok( thrown, "Exception thrown when trying to change type property" );
150 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
152 var check = document.createElement("input");
155 jQuery(check).attr('type','checkbox');
159 ok( thrown, "Exception thrown when trying to change type property" );
160 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
162 var check = jQuery("<input />");
165 check.attr('type','checkbox');
169 ok( thrown, "Exception thrown when trying to change type property" );
170 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
172 var button = jQuery("#button");
175 button.attr('type','submit');
179 ok( thrown, "Exception thrown when trying to change type property" );
180 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
183 test("attr(jquery_method)", function(){
186 var $elem = jQuery("<div />"),
190 $elem.attr('html', 'foo');
191 equals( elem.innerHTML, 'foo', 'attr(html)');
193 $elem.attr('text', 'bar');
194 equals( elem.innerHTML, 'bar', 'attr(text)');
196 $elem.attr('addClass', 'css');
197 equals( elem.className, 'css', 'attr(addClass)');
199 $elem.attr('removeClass', 'css');
200 equals( jQuery.trim(elem.className), '', 'attr(removeClass)');
202 $elem.attr('css', {color:'red'});
203 ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
205 $elem.attr('height', 10);
206 equals( elem.style.height, '10px', 'attr(height)');
208 $elem.attr('each', function(){
210 ok(true, 'attr(each)');
214 // Multiple attributes
218 css:{ paddingLeft:1, paddingRight:1 }
221 equals( elem.style.width, '10px', 'attr({...})');
222 equals( elem.style.paddingLeft, '1px', 'attr({...})');
223 equals( elem.style.paddingRight, '1px', 'attr({...})');
227 test("attr(String, Object) - Loaded via XML document", function() {
230 jQuery.get('data/dashboard.xml', function(xml) {
232 jQuery('tab', xml).each(function() {
233 titles.push(jQuery(this).attr('title'));
235 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
236 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
242 test("attr('tabindex')", function() {
245 // elements not natively tabbable
246 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
247 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
250 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
251 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
252 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
254 // anchor without href
255 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
256 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
257 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
260 test("attr('tabindex', value)", function() {
263 var element = jQuery('#divWithNoTabIndex');
264 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
266 // set a positive string
267 element.attr('tabindex', '1');
268 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
271 element.attr('tabindex', '0');
272 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
274 // set a negative string
275 element.attr('tabindex', '-1');
276 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
278 // set a positive number
279 element.attr('tabindex', 1);
280 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
283 element.attr('tabindex', 0);
284 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
286 // set a negative number
287 element.attr('tabindex', -1);
288 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
290 element = jQuery('#linkWithTabIndex');
291 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
293 element.attr('tabindex', -1);
294 equals(element.attr('tabindex'), -1, 'set negative tabindex');
297 test("addClass(String)", function() {
299 var div = jQuery("div");
300 div.addClass("test");
302 for ( var i = 0; i < div.size(); i++ ) {
303 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
305 ok( pass, "Add Class" );
307 // using contents will get regular, text, and comment nodes
308 var j = jQuery("#nonnodes").contents();
310 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
313 test("removeClass(String) - simple", function() {
316 var $divs = jQuery('div');
318 $divs.addClass("test").removeClass("test");
320 ok( !$divs.is('.test'), "Remove Class" );
323 $divs = jQuery('div');
325 $divs.addClass("test").addClass("foo").addClass("bar");
326 $divs.removeClass("test").removeClass("bar").removeClass("foo");
328 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
331 $divs = jQuery('div');
333 // Make sure that a null value doesn't cause problems
334 $divs.eq(0).addClass("test").removeClass(null);
335 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
337 $divs.eq(0).addClass("test").removeClass("");
338 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
340 // using contents will get regular, text, and comment nodes
341 var j = jQuery("#nonnodes").contents();
342 j.removeClass("asdf");
343 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
346 test("toggleClass(String|boolean|undefined[, boolean])", function() {
349 var e = jQuery("#firstp");
350 ok( !e.is(".test"), "Assert class not present" );
351 e.toggleClass("test");
352 ok( e.is(".test"), "Assert class present" );
353 e.toggleClass("test");
354 ok( !e.is(".test"), "Assert class not present" );
356 // class name with a boolean
357 e.toggleClass("test", false);
358 ok( !e.is(".test"), "Assert class not present" );
359 e.toggleClass("test", true);
360 ok( e.is(".test"), "Assert class present" );
361 e.toggleClass("test", false);
362 ok( !e.is(".test"), "Assert class not present" );
364 // multiple class names
365 e.addClass("testA testB");
366 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
367 e.toggleClass("testB testC");
368 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
369 e.toggleClass("testA testC");
370 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
372 // toggleClass storage
374 ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
375 e.addClass("testD testE");
376 ok( e.is(".testD.testE"), "Assert class present" );
378 ok( !e.is(".testD.testE"), "Assert class not present" );
379 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
381 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
382 e.toggleClass(false);
383 ok( !e.is(".testD.testE"), "Assert class not present" );
385 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
387 e.toggleClass(false);
389 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
394 e.removeClass("testD");
395 e.removeData('__className__');
398 test("removeAttr(String", function() {
400 equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
403 test("addClass, removeClass, hasClass", function() {
406 var jq = jQuery("<p>Hi</p>"), x = jq[0];
409 equals( x.className, "hi", "Check single added class" );
411 jq.addClass("foo bar");
412 equals( x.className, "hi foo bar", "Check more added classes" );
415 equals( x.className, "", "Remove all classes" );
417 jq.addClass("hi foo bar");
418 jq.removeClass("foo");
419 equals( x.className, "hi bar", "Check removal of one class" );
421 ok( jq.hasClass("hi"), "Check has1" );
422 ok( jq.hasClass("bar"), "Check has2" );