/    Sign up×
Community /Pin to ProfileBookmark

radio checked function

Hello,

I have a function that I call that tells me the checked value of a group of radio boxes. You pass in the form name and radio group name and it gives you the value of the one that is checked.

I have a dynamic list of existing shipto addresses that a user can select one in order to have something shipped to a previously used shipto address. If I have more than one, my code works but if there is only one radio option and they click it, the object is deemed undefined. I need for this function to handle a single radio option on the page as it is possible in some cases.

Here is my code

[CODE]
function get_radio_checked_value( form_name, radio_name )
{
var radioObj = document.forms[ form_name ].elements[ radio_name ];
for(var i = 0; i < radioObj.length; i++)
{
if(radioObj[i].checked)
return radioObj[i].value;
}
}[/CODE]

And my usage is usually like this

[CODE]var tempradio = get_radio_checked_value( ‘frm_shipto’, ‘rad_shipto’ );[/CODE]

In the above example if I have more than one address / radio option, tempradio holds the correct value otherwise if just one address available it holds undefined

Any help would be greatly appreciated.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@jamesbcox1980Nov 11.2009 — You do 2 things differently:

  • 1. use document.getElementsByName(radio_name) instead of document.forms[ form_name ].elements[ radio_name ] to return create a proper array collection in the event that there is only 1 radio. I [I]think[/I] that having only 1 input makes elements[xxx] refer to an actual object instead of an HTML collection.


  • 2. In case the loop continues to the end, you'll need to use "return false" to make sure your variable is set as something. Otherwise, if none are checked, you'll still get undefined.


  • The new version:
    [CODE]function get_radio_checked_value(radio_name){
    radioObj = document.getElementsByName(radio_name);
    for(var i = 0; i < radioObj.length;i++){
    if(radioObj[i].checked) {return radioObj[i].value;}
    }
    return false;
    }[/CODE]

    Usage:
    [CODE]
    var tempradio = get_radio_checked_value('rad_shipto');
    [/CODE]


    I tested this on my own HTML and it worked. If it doesn't, let me know.
    Copy linkTweet thisAlerts:
    @tripwaterauthorNov 11.2009 — I found my answer, thanks

    [CODE]
    var radioLength = radioObj.length;
    ( radioLength == undefined )
    if( radioObj.checked )
    return radioObj.value;
    else
    return "";
    [/CODE]
    Copy linkTweet thisAlerts:
    @jamesbcox1980Nov 11.2009 — I found my answer, thanks

    [CODE]
    var radioLength = radioObj.length;
    ( radioLength == undefined )
    if( radioObj.checked )
    return radioObj.value;
    else
    return "";
    [/CODE]
    [/QUOTE]


    Actually, that's not a solution. It's a work-around for an error in your code ?
    Copy linkTweet thisAlerts:
    @tripwaterauthorNov 11.2009 — You must have been submitting your post just as I was and mine hit after. I found my solution or work around because I was in a hurry to move on to the next thing at my job but I like your solution much, much better. It is cleaner and more to the point.

    Thanks for the reply.
    Copy linkTweet thisAlerts:
    @jamesbcox1980Nov 11.2009 — I figured as much... I just thought I'd mess with you a little:p
    Copy linkTweet thisAlerts:
    @jamesbcox1980Nov 11.2009 — Oh, one more thing... don't use "var" for declaring variables inside of functions unless you need the variable to work globally--that is, unless you need other parts of the script to be able to access the variable. I didn't notice this the first time around when examining your code. I saw the first one, but I didn't catch the one in your for() loop. So here is a minor revision:
    [CODE]
    function get_radio_checked_value(radio_name){
    radioObj=document.getElementsByName(radio_name);
    for(i=0;i<radioObj.length;i++){
    if(radioObj[i].checked){return radioObj[i].value;}
    }
    return false;
    }
    [/CODE]
    ×

    Success!

    Help @tripwater spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.8,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...