+ Reply to Thread
Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By sikuneh
  • 1 Post By kbjradmin

Thread: Javascript and radio buttons help

  1. #1
    sikuneh is offline x10Hosting Member sikuneh is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    34

    Javascript and radio buttons help

    I have been having some trouble with my javascript that involves radio buttons. I have no idea how to check if either radio A or radio B have been selected.

    My guess:
    JS
    Code:
    <script type="text/javascript">
    function validate(radios) {
     var confirm = document.getElementsByName(radios).value;
     var button = document.getElementById("button");
     button.disabled = true;
     if(confirm == '0')
      button.disabled = false;
     else
      button.disabled = true;
    }
    </script>
    HTML Code:
    <form action="#" method="post" onchange="validate('confirm')">
    No <input type="radio" name="confirm" value="0" /><br />
    Yes <input type="radio" name="confirm" value="1" /><br />
    <input type="submit" value="Go" id="button" />
    </form>
    dinomirt96 likes this.

  2. #2
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Re: Javascript and radio buttons help

    Code:
    // these are your radio buttons
    var
    radio1 = document.getElementById('radio1'),
    radio2 = document.getElementById('radio2');
    // check if one of them is selected
    var which = (radio1.checked) ? 'radio1' : 'radio2';
    // tell you which is selected
    alert(which);
    i think that's right.

  3. #3
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: Javascript and radio buttons help

    So if you were to try to check if either were selected would this work?

    Code:
    var
    radio1 = document.getElementById("radio1");
    radio2 = document.getElementById("radio2");
    check1 = (radio1.checked) ? 'radio1' : false;
    check2 = (radio2.checked) ? 'radio2' : false;
    error = document.getElementById("error");
    if(check1 != false && check2 != false)
    error.innerHTML = "";
    else
    error.innerHTML = "Please choose one.";
    Last edited by as4s1n; 03-17-2010 at 04:24 PM.

  4. #4
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Re: Javascript and radio buttons help

    yes, that should work; however, a caution, on the second line, I ended it with a comma (, ) not a semi-colon(; ). the comma tells it that the "var" statement isn't over, the semi-colon, though, ends the "var". This can have bad effects depending on what the rest of your code looks like. I would suggest changing that back to a comma.
    Last edited by kbjradmin; 03-17-2010 at 04:52 PM.
    karimirt47 likes this.

  5. #5
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: Javascript and radio buttons help

    Oh, I guess I overlooked that, Duly noted.

  6. #6
    misson is offline Community Advocate misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,391

    Re: Javascript and radio buttons help

    Note that you can also get inputs via the form element, which is helpful if you have more than two radio buttons in a group.
    HTML Code:
    <form name="colorForm">
      <input type="radio" name="color" value="red" id="color_red"/><label for="color_red">Red</label>
      <input type="radio" name="color" value="green" id="color_green"/><label for="color_green">Green</label>
      <input type="radio" name="color" value="blue" id="color_blue"/><label for="color_blue">Blue</label>
    </form>
    <script type="text/javascript">
        var colors = document.forms.colorForm.elements.color,
            colorElt;
        var i;
        for (i=0; i < colors.length; ++i) {
            if (colors[i].checked) {
                colorElt = colors[i];
                break;
            }
        }
    </script>
    You can also track which radio button is selected by adding change or click handlers to the radio buttons.
    HTML Code:
    <form name="colorForm">
      <input type="radio" name="color" value="red" id="color_red"/><label for="color_red">Red</label>
      <input type="radio" name="color" value="green" id="color_green"/><label for="color_green">Green</label>
      <input type="radio" name="color" value="blue" id="color_blue"/><label for="color_blue">Blue</label>
    </form>
    <script type="text/javascript">
        function trackSelectionOf(watcher, inputs) {
            function createSelectionTracker(elt) {
                return function () {
                    watcher.selected[elt.name] = elt;
                }
            }
    
            if (typeof watcher.selected == 'undefined') {
                watcher.selected = {};
            }
            var tracker;
            for (i=0; i < inputs.length; ++i) {
                /* Note: you'll have to simulate addEventListener in IE using attachEvent, 
                   or use a library that does it for you
                 */
                inputs[i].addEventListener('change', 
                                           tracker=createSelectionTracker(inputs[i]),
                                           false);
                if (inputs[i].checked) {
                    tracker();
                }
            }
        }
    
        function trackSelection(form) {
            var done={},
                input;
            for (i=0; i < form.elements.length; ++i) {
                input = form.elements[i];
                if (!done[input.name] && input.type == 'radio') {
                    done[input.name] = true;
                    trackSelectionOf(form, form.elements[input.name]);
                }
            }
        }
        trackSelection(document.forms.colorForm);
    
      /* current selection for colorForm.color can be accessed as 
         document.forms.colorForm.selected.colors
       */
    </script>
    Last edited by misson; 03-17-2010 at 09:22 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Ice Radio - Online Radio
    By Scott in forum Advertising
    Replies: 6
    Last Post: 09-19-2008, 02:20 AM
  2. Visitor Counter, Calculated Fields, and Radio Buttons
    By zhangh99 in forum Scripts & 3rd Party Apps
    Replies: 6
    Last Post: 06-20-2008, 11:08 AM
  3. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 01:41 AM
  4. Check Boxes and Radio Buttons Conquered by DHTML
    By Nathan in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 07-13-2005, 06:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers