|
From: mad.scientist.jr on 21 Apr 2008 12:21 According to http://www.quirksmode.org/js/forms.html you need to look through a radio button's members to find the value: >Radio buttons >Unfortunately it's not possible to see at once which radio button in a group the user has checked. You need to go >through all radio's and see which one's checked property is true. I tried writing a reusable function to return the value, see code at: http://www.geocities.com/usenet_daughter/javascript_radio.htm but for some reason I can only get a value if I hard code the control name. Can anyone explain how to do this? Thanks
From: Tom Cole on 21 Apr 2008 14:06 On Apr 21, 12:21 pm, mad.scientist...(a)gmail.com wrote: > According to > http://www.quirksmode.org/js/forms.html > you need to look through a radio button's members to find the value: > > >Radio buttons > >Unfortunately it's not possible to see at once which radio button in a group the user has checked. You need to go >through all radio's and see which one's checked property is true. > > I tried writing a reusable function to return the value, see code at: > http://www.geocities.com/usenet_daughter/javascript_radio.htm > > but for some reason I can only get a value if I hard code the control > name. > > Can anyone explain how to do this? > > Thanks Does this work for you? function getSelectedValueForName(name) { for (var i = 0; i < document.getElementsByTagName("input").length; i+ +) { var input = document.getElementsByTagName("input")[i]; if (input.type == "radio" && input.checked) { return input.value; } } return null; } You would call getSelectedValueForName('Radio1'); where your radio buttons share the name Radio1. HTH.
From: Peroli on 22 Apr 2008 10:16 Tom Cole wrote: > On Apr 21, 12:21�pm, mad.scientist...(a)gmail.com wrote: > > According to > > � �http://www.quirksmode.org/js/forms.html > > you need to look through a radio button's members to find the value: > > > > >Radio buttons > > >Unfortunately it's not possible to see at once which radio button in a group the user has checked. You need to go >through all radio's and see which one's checked property is true. > > > > I tried writing a reusable function to return the value, see code at: > > � �http://www.geocities.com/usenet_daughter/javascript_radio.htm > > > > but for some reason I can only get a value if I hard code the control > > name. > > > > Can anyone explain how to do this? > > > > Thanks > > Does this work for you? > > function getSelectedValueForName(name) { > for (var i = 0; i < document.getElementsByTagName("input").length; i+ > +) { > var input = document.getElementsByTagName("input")[i]; > if (input.type == "radio" && input.checked) { > return input.value; > } > } > return null; > } > > You would call getSelectedValueForName('Radio1'); where your radio > buttons share the name Radio1. > > HTH. Tim, HTH, Your solution was good, but from performance point of view, you can do better. Caching your "input" collection will greatly improve speed. Also, for loops will calculate the collection's length for each iteration. function getSelectedValueForName(name) { var inputs = document.getElementsByTagName("input"); for (var i = 0, len = inputs.length; i < len; i++) { if (inputs[i].type == "radio" && input[i].checked) { return input[i].value; } } return null; } Even better, if you use Prototype JS Library. var checked_values = $$ ('input:checked[type="radio"]').pluck('value'); -- Peroli Sivaprakasam
From: Tom Cole on 22 Apr 2008 12:15 On Apr 22, 10:16 am, Peroli <per...(a)gmail.com> wrote: > Tom Cole wrote: > > On Apr 21, 12:21�pm, mad.scientist...(a)gmail.com wrote: > > > According to > > > � �http://www.quirksmode.org/js/forms.html > > > you need to look through a radio button's members to find the value: > > > > >Radio buttons > > > >Unfortunately it's not possible to see at once which radio button in a group the user has checked. You need to go >through all radio's and see which one's checked property is true. > > > > I tried writing a reusable function to return the value, see code at: > > > � �http://www.geocities.com/usenet_daughter/javascript_radio.htm > > > > but for some reason I can only get a value if I hard code the control > > > name. > > > > Can anyone explain how to do this? > > > > Thanks > > > Does this work for you? > > > function getSelectedValueForName(name) { > >   for (var i = 0; i < document.getElementsByTagName("input").length; i+ > > +) { > >       var input = document.getElementsByTagName("input")[i]; > >       if (input.type == "radio" && input.checked) { > >           return input.value; > >       } > >   } > >   return null; > > } > > > You would call getSelectedValueForName('Radio1'); where your radio > > buttons share the name Radio1. > > > HTH. > > Tim, HTH, >   Your solution was good, but from performance point of view, you > can do better. > Caching your "input" collection will greatly improve speed. Also, for > loops will calculate > the collection's length for each iteration. > > function getSelectedValueForName(name) { >     var inputs = document.getElementsByTagName("input"); >     for (var i = 0, len = inputs.length; i < len; i++) { >         if (inputs[i].type == "radio" && input[i].checked) { s/b if (inputs[i].type == "radio" && inputs[i].checked) { >             return input[i].value; s/b return inputs[i].value; >         } >     } >     return null; > > } > I'm not convinced that using inputs[i] three times is more efficient than a single assignment of var input = document.getElementsByTagName('input')[i]. Maybe someone with better understanding of what goes on behind the scenes could elaborate. I do see your point though of not calling .length for each iteration. > Even better, if you use Prototype JS Library. > >   var checked_values = $$ > ('input:checked[type="radio"]').pluck('value'); > > -- Peroli Sivaprakasam- Hide quoted text - > > - Show quoted text - Better for whom? Fewer lines of code in the OP's code? Maybe, but it doesn't mean it'll perform better or be more readable code. What does the $$ and pluck methods in Prototype look like?
From: Evertjan. on 22 Apr 2008 13:22 Peroli wrote on 22 apr 2008 in comp.lang.javascript: > function getSelectedValueForName(name) { > var inputs = document.getElementsByTagName("input"); > for (var i = 0, len = inputs.length; i < len; i++) { > if (inputs[i].type == "radio" && input[i].checked) { > return input[i].value; > } > } > return null; >} > Where is the parameter called name used? If the inputs have a common name in a form do: var inputs = document.forms['myForm'].elements[name]; -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
|
Next
|
Last
Pages: 1 2 Prev: " in textfields Next: Windows XP Command Prompt - called via ActiveXObject |