![]() |
Radio & Checkbox & Drop Down values
Continuation of a resolved thread: http://www.codingforums.com/showthread.php?t=284053
I tried making a test HTML for 'OldPedant's post #5 in the referenced link above. In the process, I tried using the same type of logic to a single & multiple select box. It seems to work, but I would like an evaluation of the logic for the 'getSelectValue()' function. Is it a good way to perform the checks? Was/is there a way to use the original 'getGroupValue()' function with the D-Downs in one common function? Code:
<!DOCTYPE HTML> |
I'll follow you to this post then..
I would correct the HTML at the beginning (in particular, to prevent IE dropping into quirks mode) so that it can be tested in all browsers: Code:
<!DOCTYPE html>Seems to work well. Again, just my personal opinion :), but I would prefer to use separate functions. I think these are trying to do too much, and if I look at this again in a couple of months I'll need to remind myself of what the functions do. Of course, function documentation (docstrings) would help with this. |
Recommended modifications
Here I have incorporated your recommendations, if of interest to anyone else...
Code:
<!DOCTYPE HTML> |
It is a shame we can't convert a nodeList into an array with [group], so I think I've just wasted 30 minutes:
Code:
var arr = []; |
Quote:
I won't pretend I understand it all, but it does appear to work as the earlier script EXCEPT for the single checkbox or radio button option. Since I believe they would be seldom used that way, and could easily be checked with ID assignments rather than groups, I'll demonstrate what the changes would look like. Code:
<!DOCTYPE HTML>Do you think it relevant enough to put into the 'Post A Javascript' section? |
Quote:
Code:
function getGroupValue( group ) /* group == radio/checkbox single/group or <select> options list */ |
Quote:
Code:
var arr = [];A DOM nodeList is like an array but is not an array - it's really a collection of objects. This code first stores (converts?) the nodeList to an array. The array map method creates another array by applying a function to every element of an array. In this instance, the new array contains either the value false, or the value of the input; that is, if it is checked. The method filter is then applied to the resultant array. This removes the false elements from the array, leaving only the values of those elements which were checked. It is not really useful for this example. It could be useful if a lot more processing is done within the map or filter methods. Andy. |
Quote:
I like the idea of one function to collect information from different element types. Less typing, less prone to errors, common implementation, understandable and neat all rolled-up into one small package. Thanks for the attention to the topic, 'Logic Ali'. Added to the "Post A JavaScript" section of this forum. See: http://www.codingforums.com/showthre...64#post1300264 |
| All times are GMT +1. The time now is 06:22 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.