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 // Fails on IE since recent changes to .attr()
98 // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
100 jQuery("#name").attr('name', 'something');
101 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
102 jQuery("#check2").attr('checked', true);
103 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
104 jQuery("#check2").attr('checked', false);
105 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
106 jQuery("#text1").attr('readonly', true);
107 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
108 jQuery("#text1").attr('readonly', false);
109 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
110 jQuery("#name").attr('maxlength', '5');
111 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
112 jQuery("#name").attr('maxLength', '10');
113 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
115 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>"),
116 td = table.find('td:first');
117 td.attr("rowspan", "2");
118 equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
119 td.attr("colspan", "2");
120 equals( td[0].colSpan, 2, "Check colspan is correctly set" );
121 table.attr("cellspacing", "2");
122 equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
125 jQuery("#name").attr('someAttr', '0');
126 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
127 jQuery("#name").attr('someAttr', 0);
128 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
129 jQuery("#name").attr('someAttr', 1);
130 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
132 // using contents will get comments regular, text, and comment nodes
133 var j = jQuery("#nonnodes").contents();
135 j.attr("name", "attrvalue");
136 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
137 j.removeAttr("name");
141 var type = jQuery("#check2").attr('type');
144 jQuery("#check2").attr('type','hidden');
148 ok( thrown, "Exception thrown when trying to change type property" );
149 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
151 var check = document.createElement("input");
154 jQuery(check).attr('type','checkbox');
158 ok( thrown, "Exception thrown when trying to change type property" );
159 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
161 var check = jQuery("<input />");
164 check.attr('type','checkbox');
168 ok( thrown, "Exception thrown when trying to change type property" );
169 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
171 var button = jQuery("#button");
174 button.attr('type','submit');
178 ok( thrown, "Exception thrown when trying to change type property" );
179 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
182 test("attr(jquery_method)", function(){
185 var $elem = jQuery("<div />"),
189 $elem.attr('html', 'foo');
190 equals( elem.innerHTML, 'foo', 'attr(html)');
192 $elem.attr('text', 'bar');
193 equals( elem.innerHTML, 'bar', 'attr(text)');
195 $elem.attr('addClass', 'css');
196 equals( elem.className, 'css', 'attr(addClass)');
198 $elem.attr('removeClass', 'css');
199 equals( jQuery.trim(elem.className), '', 'attr(removeClass)');
201 $elem.attr('css', {color:'red'});
202 equals( elem.style.color, 'red', 'attr(css)');
204 $elem.attr('height', 10);
205 equals( elem.style.height, '10px', 'attr(height)');
207 $elem.attr('each', function(){
209 ok(true, 'attr(each)');
213 // Multiple attributes
217 css:{ paddingLeft:1, paddingRight:1 }
220 equals( elem.style.width, '10px', 'attr({...})');
221 equals( elem.style.paddingLeft, '1px', 'attr({...})');
222 equals( elem.style.paddingRight, '1px', 'attr({...})');
226 test("attr(String, Object) - Loaded via XML document", function() {
229 jQuery.get('data/dashboard.xml', function(xml) {
231 jQuery('tab', xml).each(function() {
232 titles.push(jQuery(this).attr('title'));
234 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
235 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
241 test("attr('tabindex')", function() {
244 // elements not natively tabbable
245 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
246 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
249 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
250 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
251 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
253 // anchor without href
254 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
255 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
256 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
259 test("attr('tabindex', value)", function() {
262 var element = jQuery('#divWithNoTabIndex');
263 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
265 // set a positive string
266 element.attr('tabindex', '1');
267 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
270 element.attr('tabindex', '0');
271 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
273 // set a negative string
274 element.attr('tabindex', '-1');
275 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
277 // set a positive number
278 element.attr('tabindex', 1);
279 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
282 element.attr('tabindex', 0);
283 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
285 // set a negative number
286 element.attr('tabindex', -1);
287 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
289 element = jQuery('#linkWithTabIndex');
290 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
292 element.attr('tabindex', -1);
293 equals(element.attr('tabindex'), -1, 'set negative tabindex');
296 test("addClass(String)", function() {
298 var div = jQuery("div");
299 div.addClass("test");
301 for ( var i = 0; i < div.size(); i++ ) {
302 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
304 ok( pass, "Add Class" );
306 // using contents will get regular, text, and comment nodes
307 var j = jQuery("#nonnodes").contents();
309 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
312 test("removeClass(String) - simple", function() {
315 var $divs = jQuery('div');
317 $divs.addClass("test").removeClass("test");
319 ok( !$divs.is('.test'), "Remove Class" );
322 $divs = jQuery('div');
324 $divs.addClass("test").addClass("foo").addClass("bar");
325 $divs.removeClass("test").removeClass("bar").removeClass("foo");
327 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
330 $divs = jQuery('div');
332 // Make sure that a null value doesn't cause problems
333 $divs.eq(0).addClass("test").removeClass(null);
334 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
336 $divs.eq(0).addClass("test").removeClass("");
337 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
339 // using contents will get regular, text, and comment nodes
340 var j = jQuery("#nonnodes").contents();
341 j.removeClass("asdf");
342 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
345 test("toggleClass(String|boolean|undefined[, boolean])", function() {
348 var e = jQuery("#firstp");
349 ok( !e.is(".test"), "Assert class not present" );
350 e.toggleClass("test");
351 ok( e.is(".test"), "Assert class present" );
352 e.toggleClass("test");
353 ok( !e.is(".test"), "Assert class not present" );
355 // class name with a boolean
356 e.toggleClass("test", false);
357 ok( !e.is(".test"), "Assert class not present" );
358 e.toggleClass("test", true);
359 ok( e.is(".test"), "Assert class present" );
360 e.toggleClass("test", false);
361 ok( !e.is(".test"), "Assert class not present" );
363 // multiple class names
364 e.addClass("testA testB");
365 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
366 e.toggleClass("testB testC");
367 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
368 e.toggleClass("testA testC");
369 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
371 // toggleClass storage
373 ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
374 e.addClass("testD testE");
375 ok( e.is(".testD.testE"), "Assert class present" );
377 ok( !e.is(".testD.testE"), "Assert class not present" );
378 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
380 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
381 e.toggleClass(false);
382 ok( !e.is(".testD.testE"), "Assert class not present" );
384 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
386 e.toggleClass(false);
388 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
393 e.removeClass("testD");
394 e.removeData('__className__');
397 test("removeAttr(String", function() {
399 equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
402 test("addClass, removeClass, hasClass", function() {
405 var jq = jQuery("<p>Hi</p>"), x = jq[0];
408 equals( x.className, "hi", "Check single added class" );
410 jq.addClass("foo bar");
411 equals( x.className, "hi foo bar", "Check more added classes" );
414 equals( x.className, "", "Remove all classes" );
416 jq.addClass("hi foo bar");
417 jq.removeClass("foo");
418 equals( x.className, "hi bar", "Check removal of one class" );
420 ok( jq.hasClass("hi"), "Check has1" );
421 ok( jq.hasClass("bar"), "Check has2" );