From ae817b233f250d8c107b44982c3ae641e3d26222 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Wed, 26 Oct 2016 14:40:38 -0400 Subject: [PATCH 1/3] val: Remove use of nonstandard selectors Fixes gh-993 --- entries/val.xml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/entries/val.xml b/entries/val.xml index e7a82550..259a1fec 100644 --- a/entries/val.xml +++ b/entries/val.xml @@ -13,20 +13,20 @@

The .val() method is primarily used to get the values of form elements such as input, select and textarea. When called on an empty collection, it returns undefined.

When the first element in the collection is a select-multiple (i.e., a select element with the multiple attribute set), .val() returns an array containing the value of each selected option. As of jQuery 3.0, if no options are selected, it returns an empty array; prior to jQuery 3.0, it returns null.

-

For selects, checkboxes and radio buttons, you can also use the :selected and :checked selectors to get at values. For example:

+

For selects, checkboxes and radio buttons, you can also use the :checked selector to get at values. For example:


 
 // Get the value from a dropdown select
-$( "select.foo option:selected").val();
+$( "select.foo option:checked").val();
 
 // Get the value from a dropdown select even easier
 $( "select.foo" ).val();
 
 // Get the value from a checked checkbox
-$( "input:checkbox:checked" ).val();
+$( "input[type=checkbox]:checked" ).val();
 
 // Get the value from a set of radio buttons
-$( "input:radio[name=bar]:checked" ).val();
+$( "input[type=radio][name=bar]:checked" ).val();
       

Note: At present, using .val() on <textarea> elements strips carriage return characters from the browser-reported value. When this value is sent to the server via XHR, however, carriage returns are preserved (or added by browsers which do not include them in the raw value). A workaround for this issue can be achieved using a valHook as follows:

@@ -131,11 +131,11 @@ $( "input" )

Setting values using this method (or using the native value property) does not cause the dispatch of the change event. For this reason, the relevant event handlers will not be executed. If you want to execute them, you should call .trigger( "change" ) after setting the value.

The .val() method allows setting the value by passing in a function. As of jQuery 1.4, the function is passed two arguments, the current element's index and its current value:


-$( "input:text.items" ).val(function( index, value ) {
-  return value + " " + this.className;
+$( "input[type=text].tags" ).val(function( index, value ) {
+  return value.trim();
 });
       
-

This example appends the string " items" to the text inputs' values.

+

This example removes leading and trailing whitespace from the text inputs' values.

Set the value of an input box. From 38e43f5d05ef63ace91bbcaa30ee004b59759b1f Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Mon, 19 Dec 2016 11:40:24 -0500 Subject: [PATCH 2/3] val: Update per review --- entries/val.xml | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/entries/val.xml b/entries/val.xml index 259a1fec..65ba3337 100644 --- a/entries/val.xml +++ b/entries/val.xml @@ -13,20 +13,19 @@

The .val() method is primarily used to get the values of form elements such as input, select and textarea. When called on an empty collection, it returns undefined.

When the first element in the collection is a select-multiple (i.e., a select element with the multiple attribute set), .val() returns an array containing the value of each selected option. As of jQuery 3.0, if no options are selected, it returns an empty array; prior to jQuery 3.0, it returns null.

-

For selects, checkboxes and radio buttons, you can also use the :checked selector to get at values. For example:

+

For selects, checkboxes and radio buttons, you can use :checked to select the right elements. For example:


+// Get the value from the selected option in a dropdown
+$( "select#foo option:checked" ).val();
 
-// Get the value from a dropdown select
-$( "select.foo option:checked").val();
-
-// Get the value from a dropdown select even easier
-$( "select.foo" ).val();
+// Get the value from a dropdown select directly
+$( "select#foo" ).val();
 
 // Get the value from a checked checkbox
-$( "input[type=checkbox]:checked" ).val();
+$( "input[type=checkbox][name=bar]:checked" ).val();
 
 // Get the value from a set of radio buttons
-$( "input[type=radio][name=bar]:checked" ).val();
+$( "input[type=radio][name=baz]:checked" ).val();
       

Note: At present, using .val() on <textarea> elements strips carriage return characters from the browser-reported value. When this value is sent to the server via XHR, however, carriage returns are preserved (or added by browsers which do not include them in the raw value). A workaround for this issue can be achieved using a valHook as follows:

@@ -135,7 +134,7 @@ $( "input[type=text].tags" ).val(function( index, value ) { return value.trim(); }); -

This example removes leading and trailing whitespace from the text inputs' values.

+

This example removes leading and trailing whitespace from the values of text inputs with a "tags" class.

Set the value of an input box. From e96671feeeae7b698851f7881200585d2ad96a21 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Mon, 19 Dec 2016 11:41:48 -0500 Subject: [PATCH 3/3] val: Fix whitespace --- entries/val.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entries/val.xml b/entries/val.xml index 65ba3337..56ef9e34 100644 --- a/entries/val.xml +++ b/entries/val.xml @@ -134,7 +134,7 @@ $( "input[type=text].tags" ).val(function( index, value ) { return value.trim(); }); -

This example removes leading and trailing whitespace from the values of text inputs with a "tags" class.

+

This example removes leading and trailing whitespace from the values of text inputs with a "tags" class.

Set the value of an input box.