@@ -292,6 +292,92 @@ test("attr('tabindex', value)", function() {
292
292
equals ( element . attr ( 'tabindex' ) , - 1 , 'set negative tabindex' ) ;
293
293
} ) ;
294
294
295
+ test ( "val()" , function ( ) {
296
+ expect ( 17 ) ;
297
+
298
+ document . getElementById ( 'text1' ) . value = "bla" ;
299
+ equals ( jQuery ( "#text1" ) . val ( ) , "bla" , "Check for modified value of input element" ) ;
300
+
301
+ reset ( ) ;
302
+
303
+ equals ( jQuery ( "#text1" ) . val ( ) , "Test" , "Check for value of input element" ) ;
304
+ // ticket #1714 this caused a JS error in IE
305
+ equals ( jQuery ( "#first" ) . val ( ) , "" , "Check a paragraph element to see if it has a value" ) ;
306
+ ok ( jQuery ( [ ] ) . val ( ) === undefined , "Check an empty jQuery object will return undefined from val" ) ;
307
+
308
+ equals ( jQuery ( '#select2' ) . val ( ) , '3' , 'Call val() on a single="single" select' ) ;
309
+
310
+ same ( jQuery ( '#select3' ) . val ( ) , [ '1' , '2' ] , 'Call val() on a multiple="multiple" select' ) ;
311
+
312
+ equals ( jQuery ( '#option3c' ) . val ( ) , '2' , 'Call val() on a option element with value' ) ;
313
+
314
+ equals ( jQuery ( '#option3a' ) . val ( ) , '' , 'Call val() on a option element with empty value' ) ;
315
+
316
+ equals ( jQuery ( '#option3e' ) . val ( ) , 'no value' , 'Call val() on a option element with no value attribute' ) ;
317
+
318
+ equals ( jQuery ( '#option3a' ) . val ( ) , '' , 'Call val() on a option element with no value attribute' ) ;
319
+
320
+ jQuery ( '#select3' ) . val ( "" ) ;
321
+ same ( jQuery ( '#select3' ) . val ( ) , [ '' ] , 'Call val() on a multiple="multiple" select' ) ;
322
+
323
+ var checks = jQuery ( "<input type='checkbox' name='test' value='1'/>" ) . appendTo ( "#form" )
324
+ . add ( jQuery ( "<input type='checkbox' name='test' value='2'/>" ) . appendTo ( "#form" ) )
325
+ . add ( jQuery ( "<input type='checkbox' name='test' value=''/>" ) . appendTo ( "#form" ) )
326
+ . add ( jQuery ( "<input type='checkbox' name='test'/>" ) . appendTo ( "#form" ) ) ;
327
+
328
+ same ( checks . serialize ( ) , "" , "Get unchecked values." ) ;
329
+
330
+ equals ( checks . eq ( 3 ) . val ( ) , "on" , "Make sure a value of 'on' is provided if none is specified." ) ;
331
+
332
+ checks . val ( [ "2" ] ) ;
333
+ same ( checks . serialize ( ) , "test=2" , "Get a single checked value." ) ;
334
+
335
+ checks . val ( [ "1" , "" ] ) ;
336
+ same ( checks . serialize ( ) , "test=1&test=" , "Get multiple checked values." ) ;
337
+
338
+ checks . val ( [ "" , "2" ] ) ;
339
+ same ( checks . serialize ( ) , "test=2&test=" , "Get multiple checked values." ) ;
340
+
341
+ checks . val ( [ "1" , "on" ] ) ;
342
+ same ( checks . serialize ( ) , "test=1&test=on" , "Get multiple checked values." ) ;
343
+
344
+ checks . remove ( ) ;
345
+ } ) ;
346
+
347
+ var testVal = function ( valueObj ) {
348
+ expect ( 6 ) ;
349
+
350
+ jQuery ( "#text1" ) . val ( valueObj ( 'test' ) ) ;
351
+ equals ( document . getElementById ( 'text1' ) . value , "test" , "Check for modified (via val(String)) value of input element" ) ;
352
+
353
+ jQuery ( "#text1" ) . val ( valueObj ( 67 ) ) ;
354
+ equals ( document . getElementById ( 'text1' ) . value , "67" , "Check for modified (via val(Number)) value of input element" ) ;
355
+
356
+ jQuery ( "#select1" ) . val ( valueObj ( "3" ) ) ;
357
+ equals ( jQuery ( "#select1" ) . val ( ) , "3" , "Check for modified (via val(String)) value of select element" ) ;
358
+
359
+ jQuery ( "#select1" ) . val ( valueObj ( 2 ) ) ;
360
+ equals ( jQuery ( "#select1" ) . val ( ) , "2" , "Check for modified (via val(Number)) value of select element" ) ;
361
+
362
+ jQuery ( "#select1" ) . append ( "<option value='4'>four</option>" ) ;
363
+ jQuery ( "#select1" ) . val ( valueObj ( 4 ) ) ;
364
+ equals ( jQuery ( "#select1" ) . val ( ) , "4" , "Should be possible to set the val() to a newly created option" ) ;
365
+
366
+ // using contents will get comments regular, text, and comment nodes
367
+ var j = jQuery ( "#nonnodes" ) . contents ( ) ;
368
+ j . val ( valueObj ( "asdf" ) ) ;
369
+ equals ( j . val ( ) , "asdf" , "Check node,textnode,comment with val()" ) ;
370
+ j . removeAttr ( "value" ) ;
371
+ }
372
+
373
+ test ( "val(String/Number)" , function ( ) {
374
+ testVal ( bareObj ) ;
375
+ } ) ;
376
+
377
+ test ( "val(Function)" , function ( ) {
378
+ testVal ( functionReturningObj ) ;
379
+ } )
380
+
295
381
var testAddClass = function ( valueObj ) {
296
382
expect ( 2 ) ;
297
383
var div = jQuery ( "div" ) ;
0 commit comments