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("addClass(String)", function() {
237 var div = jQuery("div");
238 div.addClass("test");
240 for ( var i = 0; i < div.size(); i++ ) {
241 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
243 ok( pass, "Add Class" );
245 // using contents will get regular, text, and comment nodes
246 var j = jQuery("#nonnodes").contents();
248 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
251 test("removeClass(String) - simple", function() {
254 var $divs = jQuery('div');
256 $divs.addClass("test").removeClass("test");
258 ok( !$divs.is('.test'), "Remove Class" );
262 $divs.addClass("test").addClass("foo").addClass("bar");
263 $divs.removeClass("test").removeClass("bar").removeClass("foo");
265 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
269 // Make sure that a null value doesn't cause problems
270 $divs.eq(0).addClass("test").removeClass(null);
271 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
273 $divs.eq(0).addClass("test").removeClass("");
274 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
276 // using contents will get regular, text, and comment nodes
277 var j = jQuery("#nonnodes").contents();
278 j.removeClass("asdf");
279 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
282 test("toggleClass(String)", function() {
284 var e = jQuery("#firstp");
285 ok( !e.is(".test"), "Assert class not present" );
286 e.toggleClass("test");
287 ok( e.is(".test"), "Assert class present" );
288 e.toggleClass("test");
289 ok( !e.is(".test"), "Assert class not present" );
291 e.toggleClass("test", false);
292 ok( !e.is(".test"), "Assert class not present" );
293 e.toggleClass("test", true);
294 ok( e.is(".test"), "Assert class present" );
295 e.toggleClass("test", false);
296 ok( !e.is(".test"), "Assert class not present" );
299 test("removeAttr(String", function() {
301 equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
304 test("jQuery.className", function() {
306 var x = jQuery("<p>Hi</p>")[0];
307 var c = jQuery.className;
309 equals( x.className, "hi", "Check single added class" );
311 equals( x.className, "hi foo bar", "Check more added classes" );
313 equals( x.className, "", "Remove all classes" );
314 c.add(x, "hi foo bar");
316 equals( x.className, "hi bar", "Check removal of one class" );
317 ok( c.has(x, "hi"), "Check has1" );
318 ok( c.has(x, "bar"), "Check has2" );