Link to home
Start Free TrialLog in
Avatar of annie613
annie613

asked on

java script clear all function for check and radios

in a previous posting https://www.experts-exchange.com/questions/21476306/selectAll-function-using-radios-and-checkboxes.html
i was asking help to selectAll check boxes and radio buttons

and both members gave me insightful and correct information

but now as im moving along im getting stumped again

i thought that checkboxes and radio buttons are toggle buttons????

i have a link on my application to select all and it works
but i also want to click that same link to have the items in the checkbox clear all as well

do i need to code another function called clearAll
and another link such as this
or should this toggle on it own????

if (appList.get(1).equals("R")) // click the R link to select All
{
    headerValue = "<a href=\"#\" onclick=\"SelectAll('R')\">R</a>";
}

// i want to be able to click the same R link to clearAll

is this possible?????

and would my ClearAll function look similar to my selectAll

//posted below

<script language="JavaScript" type="Text/JavaScript">
function SelectAll()
{
var frm = document.frm
var el = frm.elements

   
    for ( i = 0; i < el.length; i++)
    {
      if (el[i].type == "checkbox")
      {
        el[i].selected = el[i].checked = true;
      }
      else (el[i]. type == "radio")//trying to do this with radios too -- working somewhat as of now
      {
        el[i].selected = el[i].checked = true;
      }
    }
}
function ClearAll()
{
  var frm = document.frm
  var el = frm.elements
 
  for ( i = 0; i < el.length; i++)
  {
    if (el[i].type == "checkbox")
    {
      el[i].selected = el[i].checked = false;
    }
   
  }
}

</script>
SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of annie613
annie613

ASKER

well im more concerned with the clearAll

i have the selectAll function selecting all the checkboxes when i click onthe link R

i want to be able to click on the same link R and clear all the checkboxes???

is that possible or do i need to put in another link called clearAll and use the ClearAll function


as for the radio buttons i have 3 links called A B C when I click on link A i want it to select all the A radios from the group
same if i click B or C

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks hackman the code looks good
my final question to this toggle select all question is this

my form is set up as a table and in one column i have a lable and checkbox with about 20 elements
and in the second column i again have a lable and checkbox with 4 elements (this are child apps to the first column)

when i selectAll it works
but i never want it to select those 4 child checkboxes unless the user manually does it

i have some code here
and commented out what i was trying to do since its not really working yet

can anyone make a suggestion

  function SelectAll()
  {
    var frm = document.frm
    var el = frm.elements
   
    //var inputSize =(document.forms[0].elements.length - 4) - <%= (childList.size() / 2) %>;

    //for( k = 0; k < lessChild.length; k++)
    //{

      //elementValue = document.forms[0].elements[k].value;
      //elementValue = elementValue.substring(2, elementValue.length);
     
      for ( i = 0; i < el.length; i++)
      {
        if (el[i].type == "checkbox")
        {
          el[i].selected = el[i].checked = true;
        }
        else (el[i].type == "radio")
        {
          el[i].selected = el[i].checked = true;
        }
      }
    //}
  }//end SelectAll()
>well im more concerned with the clearAll

My code copies the checked value of the checkAll checkbox, to all appropriate checkboxes. Hence, when you check the check all, it checks all, when you uncheck the checkall, it unchecks.

-r-
thanks roonaan and hackman! :) cheers!!!!
Thanks for the points =)
working result!!!


<script language="JavaScript" type="Text/JavaScript">
  var boxChecked = false;

  function SelectAll(selectedValue)
  {
    var frm = document.frm
    var el = frm.elements
   
    for ( i = 0; i < el.length; i++)
    {
      if (el[i].type == "checkbox")
      {
        if (boxChecked == true)
        {
          el[i].checked = false;
        }
        else
        {
          el[i].checked = true;
        }
      }

      if (el[i].type == "radio")
      {
        var priv = el[i].value;
        priv = priv.substring(2, priv.length);
       
        if (priv == selectedValue)
        {
          el[i].checked = true;
        }
        else
        {
          el[i].checked = false;
        }        
      }      
    }
   
    if (boxChecked == true)
    {
      boxChecked = false;
    }
    else
    {
      boxChecked = true;
    }
  }//end SelectAll()
 
  </script>