#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    124
    Rep Power
    17

    Status of radio group


    I know that with:

    Code:
    return ($('input[type=radio]:checked').size() > 0);
    ...I can tell if at least one radio is selected, but is there a way to focus the check on a specific radio group? Without some sort of cumbersome loop?

    And, why doesn't THIS DS textarea wrap?
  2. #2
  3. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,046
    Rep Power
    9616
    Those [] act as filters so you can add one for the name:
    Code:
    'input[type=radio][name=specificradiogroup]:checked'
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    124
    Rep Power
    17

    Still problems


    I'm not really having a lot of luck here, probably because I have no idea what I'm doing. I'm not sure why this shouldn't work:

    Code:
    var checked = false, radios = document.getElementsById('MyRadioGroup');
    for (var i = 0, radio; radio = radios[i]; i++) {
        if (radio.checked) {        
            if (confirm("Are you sure you want continue?") == true) {
                // Do something
            }
            break;
        }
    }
    
    if (!checked) {
        alert("Please select an option");
    }
    The objective is to NOT do something if an option hasn't been selected....
  6. #4
  7. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,046
    Rep Power
    9616
    getElementsById isn't a function... Do you mean getElementsByName? You're also not setting checked=true inside that one if.

    But you dropped the jQuery stuff from before?
    Code:
    if ($("input[type=checkbox][name=MyRadioGroup]:checked").size() > 0) {
    	// if continue {
    		// do something
    	// }
    } else {
    	// please select an option
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Location
    Ahmedabad
    Posts
    17
    Rep Power
    0
    Hi,

    you can use the below code and check the status of radion button

    Code:
    $(document).ready(
    function(){
      $('#nextButton').click(
        function(){
    
        var radioName = something; // use this if there's a relationship between the element
                                   // triggering this check and the radio buttons
    
          if ($('input[name='+ radioName +']:checked').length) {
               // at least one of the radio buttons was checked
               return true; // allow whatever action would normally happen to continue
          }
          else {
               // no radio button was checked
               return false; // stop whatever action would normally happen
          }
        }
      );
    }
    );
    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo