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" );
79 test("attr(String, Object)", function() {
81 var div = jQuery("div").attr("foo", "bar"),
83 for ( var i = 0; i < div.size(); i++ ) {
84 if ( div.get(i).getAttribute('foo') != "bar" ){
89 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
91 ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
93 jQuery("#name").attr('name', 'something');
94 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
95 jQuery("#check2").attr('checked', true);
96 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
97 jQuery("#check2").attr('checked', false);
98 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
99 jQuery("#text1").attr('readonly', true);
100 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
101 jQuery("#text1").attr('readonly', false);
102 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
103 jQuery("#name").attr('maxlength', '5');
104 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
105 jQuery("#name").attr('maxLength', '10');
106 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
109 jQuery("#name").attr('someAttr', '0');
110 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
111 jQuery("#name").attr('someAttr', 0);
112 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
113 jQuery("#name").attr('someAttr', 1);
114 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
116 // using contents will get comments regular, text, and comment nodes
117 var j = jQuery("#nonnodes").contents();
119 j.attr("name", "attrvalue");
120 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
121 j.removeAttr("name");
125 var type = jQuery("#check2").attr('type');
128 jQuery("#check2").attr('type','hidden');
132 ok( thrown, "Exception thrown when trying to change type property" );
133 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
135 var check = document.createElement("input");
138 jQuery(check).attr('type','checkbox');
142 ok( thrown, "Exception thrown when trying to change type property" );
143 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
145 var check = jQuery("<input />");
148 check.attr('type','checkbox');
152 ok( thrown, "Exception thrown when trying to change type property" );
153 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
155 var button = jQuery("#button");
158 button.attr('type','submit');
162 ok( thrown, "Exception thrown when trying to change type property" );
163 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
167 test("attr(String, Object) - Loaded via XML document", function() {
170 jQuery.get('data/dashboard.xml', function(xml) {
172 jQuery('tab', xml).each(function() {
173 titles.push(jQuery(this).attr('title'));
175 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
176 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
182 test("attr('tabindex')", function() {
185 // elements not natively tabbable
186 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
187 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
190 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
191 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
192 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
194 // anchor without href
195 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
196 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
197 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
200 test("attr('tabindex', value)", function() {
203 var element = jQuery('#divWithNoTabIndex');
204 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
206 // set a positive string
207 element.attr('tabindex', '1');
208 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
211 element.attr('tabindex', '0');
212 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
214 // set a negative string
215 element.attr('tabindex', '-1');
216 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
218 // set a positive number
219 element.attr('tabindex', 1);
220 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
223 element.attr('tabindex', 0);
224 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
226 // set a negative number
227 element.attr('tabindex', -1);
228 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
230 element = jQuery('#linkWithTabIndex');
231 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
233 element.attr('tabindex', -1);
234 equals(element.attr('tabindex'), -1, 'set negative tabindex');
237 test("addClass(String)", function() {
239 var div = jQuery("div");
240 div.addClass("test");
242 for ( var i = 0; i < div.size(); i++ ) {
243 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
245 ok( pass, "Add Class" );
247 // using contents will get regular, text, and comment nodes
248 var j = jQuery("#nonnodes").contents();
250 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
253 test("removeClass(String) - simple", function() {
256 var $divs = jQuery('div');
258 $divs.addClass("test").removeClass("test");
260 ok( !$divs.is('.test'), "Remove Class" );
264 $divs.addClass("test").addClass("foo").addClass("bar");
265 $divs.removeClass("test").removeClass("bar").removeClass("foo");
267 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
271 // Make sure that a null value doesn't cause problems
272 $divs.eq(0).addClass("test").removeClass(null);
273 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
275 $divs.eq(0).addClass("test").removeClass("");
276 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
278 // using contents will get regular, text, and comment nodes
279 var j = jQuery("#nonnodes").contents();
280 j.removeClass("asdf");
281 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
284 test("toggleClass(String)", function() {
286 var e = jQuery("#firstp");
287 ok( !e.is(".test"), "Assert class not present" );
288 e.toggleClass("test");
289 ok( e.is(".test"), "Assert class present" );
290 e.toggleClass("test");
291 ok( !e.is(".test"), "Assert class not present" );
293 e.toggleClass("test", false);
294 ok( !e.is(".test"), "Assert class not present" );
295 e.toggleClass("test", true);
296 ok( e.is(".test"), "Assert class present" );
297 e.toggleClass("test", false);
298 ok( !e.is(".test"), "Assert class not present" );
301 test("removeAttr(String", function() {
303 equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
306 test("jQuery.className", function() {
308 var x = jQuery("<p>Hi</p>")[0];
309 var c = jQuery.className;
311 equals( x.className, "hi", "Check single added class" );
313 equals( x.className, "hi foo bar", "Check more added classes" );
315 equals( x.className, "", "Remove all classes" );
316 c.add(x, "hi foo bar");
318 equals( x.className, "hi bar", "Check removal of one class" );
319 ok( c.has(x, "hi"), "Check has1" );
320 ok( c.has(x, "bar"), "Check has2" );