3 test("attr(String)", function() {
5 equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
6 equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
7 equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
8 equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );
9 equals( jQuery('#check1').attr('type'), "checkbox", 'Check for type attribute' );
10 equals( jQuery('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );
11 equals( jQuery('#google').attr('title'), "Google!", 'Check for title attribute' );
12 equals( jQuery('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );
13 equals( jQuery('#en').attr('lang'), "en", 'Check for lang attribute' );
14 equals( jQuery('#simon').attr('class'), "blog link", 'Check for class attribute' );
15 equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );
16 equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );
17 ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
18 equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
19 equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
20 equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
21 equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
22 equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
23 equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
25 jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
26 equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
28 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
31 // Related to [5574] and [5683]
32 var body = document.body, $body = jQuery(body);
34 ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
35 ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
37 body.setAttribute('foo', 'baz');
38 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
41 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
43 $body.attr('foo','cool');
44 equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
47 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
49 body.removeAttribute('foo'); // Cleanup
53 test("attr(String) in XML Files", function() {
56 jQuery.get("data/dashboard.xml", function(xml) {
57 equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
58 equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
64 test("attr(String, Function)", function() {
66 equals( jQuery('#text1').attr('value', function() { return this.id })[0].value, "text1", "Set value from id" );
67 equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
70 test("attr(Hash)", function() {
73 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
74 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
76 ok( pass, "Set Multiple Attributes" );
77 equals( jQuery('#text1').attr({'value': function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
78 equals( jQuery('#text1').attr({'title': function(i) { return i; }}).attr('title'), "0", "Set attribute to computed value #2");
82 test("attr(String, Object)", function() {
84 var div = jQuery("div").attr("foo", "bar"),
86 for ( var i = 0; i < div.size(); i++ ) {
87 if ( div.get(i).getAttribute('foo') != "bar" ){
92 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
94 ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
96 jQuery("#name").attr('name', 'something');
97 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
98 jQuery("#check2").attr('checked', true);
99 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
100 jQuery("#check2").attr('checked', false);
101 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
102 jQuery("#text1").attr('readonly', true);
103 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
104 jQuery("#text1").attr('readonly', false);
105 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
106 jQuery("#name").attr('maxlength', '5');
107 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
108 jQuery("#name").attr('maxLength', '10');
109 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
111 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>"),
112 td = table.find('td:first');
113 td.attr("rowspan", "2");
114 equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
115 td.attr("colspan", "2");
116 equals( td[0].colSpan, 2, "Check colspan is correctly set" );
117 table.attr("cellspacing", "2");
118 equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
121 jQuery("#name").attr('someAttr', '0');
122 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
123 jQuery("#name").attr('someAttr', 0);
124 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
125 jQuery("#name").attr('someAttr', 1);
126 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
128 // using contents will get comments regular, text, and comment nodes
129 var j = jQuery("#nonnodes").contents();
131 j.attr("name", "attrvalue");
132 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
133 j.removeAttr("name");
137 var type = jQuery("#check2").attr('type');
140 jQuery("#check2").attr('type','hidden');
144 ok( thrown, "Exception thrown when trying to change type property" );
145 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
147 var check = document.createElement("input");
150 jQuery(check).attr('type','checkbox');
154 ok( thrown, "Exception thrown when trying to change type property" );
155 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
157 var check = jQuery("<input />");
160 check.attr('type','checkbox');
164 ok( thrown, "Exception thrown when trying to change type property" );
165 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
167 var button = jQuery("#button");
170 button.attr('type','submit');
174 ok( thrown, "Exception thrown when trying to change type property" );
175 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
179 test("attr(String, Object) - Loaded via XML document", function() {
182 jQuery.get('data/dashboard.xml', function(xml) {
184 jQuery('tab', xml).each(function() {
185 titles.push(jQuery(this).attr('title'));
187 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
188 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
194 test("attr('tabindex')", function() {
197 // elements not natively tabbable
198 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
199 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
202 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
203 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
204 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
206 // anchor without href
207 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
208 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
209 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
212 test("attr('tabindex', value)", function() {
215 var element = jQuery('#divWithNoTabIndex');
216 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
218 // set a positive string
219 element.attr('tabindex', '1');
220 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
223 element.attr('tabindex', '0');
224 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
226 // set a negative string
227 element.attr('tabindex', '-1');
228 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
230 // set a positive number
231 element.attr('tabindex', 1);
232 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
235 element.attr('tabindex', 0);
236 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
238 // set a negative number
239 element.attr('tabindex', -1);
240 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
242 element = jQuery('#linkWithTabIndex');
243 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
245 element.attr('tabindex', -1);
246 equals(element.attr('tabindex'), -1, 'set negative tabindex');
249 test("addClass(String)", function() {
251 var div = jQuery("div");
252 div.addClass("test");
254 for ( var i = 0; i < div.size(); i++ ) {
255 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
257 ok( pass, "Add Class" );
259 // using contents will get regular, text, and comment nodes
260 var j = jQuery("#nonnodes").contents();
262 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
265 test("removeClass(String) - simple", function() {
268 var $divs = jQuery('div');
270 $divs.addClass("test").removeClass("test");
272 ok( !$divs.is('.test'), "Remove Class" );
276 $divs.addClass("test").addClass("foo").addClass("bar");
277 $divs.removeClass("test").removeClass("bar").removeClass("foo");
279 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
283 // Make sure that a null value doesn't cause problems
284 $divs.eq(0).addClass("test").removeClass(null);
285 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
287 $divs.eq(0).addClass("test").removeClass("");
288 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
290 // using contents will get regular, text, and comment nodes
291 var j = jQuery("#nonnodes").contents();
292 j.removeClass("asdf");
293 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
296 test("toggleClass(String|boolean|undefined[, boolean])", function() {
299 var e = jQuery("#firstp");
300 ok( !e.is(".test"), "Assert class not present" );
301 e.toggleClass("test");
302 ok( e.is(".test"), "Assert class present" );
303 e.toggleClass("test");
304 ok( !e.is(".test"), "Assert class not present" );
306 // class name with a boolean
307 e.toggleClass("test", false);
308 ok( !e.is(".test"), "Assert class not present" );
309 e.toggleClass("test", true);
310 ok( e.is(".test"), "Assert class present" );
311 e.toggleClass("test", false);
312 ok( !e.is(".test"), "Assert class not present" );
314 // multiple class names
315 e.addClass("testA testB");
316 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
317 e.toggleClass("testB testC");
318 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
319 e.toggleClass("testA testC");
320 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
322 // toggleClass storage
324 ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
325 e.addClass("testD testE");
326 ok( e.is(".testD.testE"), "Assert class present" );
328 ok( !e.is(".testD.testE"), "Assert class not present" );
329 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
331 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
332 e.toggleClass(false);
333 ok( !e.is(".testD.testE"), "Assert class not present" );
335 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
337 e.toggleClass(false);
339 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
344 e.removeClass("testD");
345 e.removeData('__className__');
348 test("removeAttr(String", function() {
350 equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
353 test("jQuery.className", function() {
355 var x = jQuery("<p>Hi</p>")[0];
356 var c = jQuery.className;
358 equals( x.className, "hi", "Check single added class" );
360 equals( x.className, "hi foo bar", "Check more added classes" );
362 equals( x.className, "", "Remove all classes" );
363 c.add(x, "hi foo bar");
365 equals( x.className, "hi bar", "Check removal of one class" );
366 ok( c.has(x, "hi"), "Check has1" );
367 ok( c.has(x, "bar"), "Check has2" );