3 Replies - 918 Views - Last Post: 09 August 2011 - 01:05 PM

#1 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Check All Class

Posted 07 August 2011 - 07:34 PM

So I have create a function that is supposed to go through and find all elements of a certain class and either check them or uncheck them all depending on another checkbox's checked value.


First my javascript function
function checkAll(obj,elmclass){

alert(elmclass);//used to debug but it doesnt pop up anything
a = document.getElementsByTagName("input");
for(i=0,i<a.length,i++){
  element=a[i];
  if (element.className == elmclass) {
    	if(obj.checked==true)
		{
		  element.checked=false;
		}else{
		  element.checked=true;
		}
    }
  }
}



and next the input with the function call

<input type="checkbox" onclick="checkAll(this,'programming')" />


I click the main check box and nothing happens. No alert and no change in check values. Am I using the properties wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Check All Class

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Check All Class

Posted 07 August 2011 - 08:37 PM

How to check Javascript for errors
Was This Post Helpful? 1
  • +
  • -

#3 JMRKER  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: Check All Class

Posted 09 August 2011 - 01:03 PM

Is this what you are trying to do?
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">

function checkAll(obj,elmclass){
  var a = document.getElementsByTagName("input");
  for (var i=0; i<a.length; i++){
    if (a[i].className == elmclass) {
      if (obj.checked==true) { a[i].checked=true; }
                        else { a[i].checked=false; }
    }
  }
}
function clearAllCBox() {
  var sel = document.getElementsByTagName('input');
  for (var i=0; i<sel.length; i++) { sel[i].checked = false; }
}
</script>
<style type="text/css">
.CBoxClass {  }
</style>

</head>
<body>
<table border="1">
 <tr><th>Non-Class</th><th>CBox Class</th></tr>
 <tr><td>
  <input type="checkbox" name="non_class"> 1
  <input type="checkbox" name="non_class"> 2
  <input type="checkbox" name="non_class"> 3
  <input type="checkbox" name="non_class"> 4
  <input type="checkbox" name="non_class"> 5
 </td> <td>
  <input type="checkbox" name="cb_class" class="CBoxClass"> A
  <input type="checkbox" name="cb_class" class="CBoxClass"> B
  <input type="checkbox" name="cb_class" class="CBoxClass"> C
  <input type="checkbox" name="cb_class" class="CBoxClass"> D
  <input type="checkbox" name="cb_class" class="CBoxClass"> E
 </td></tr>
 <tr><td>
  <input type="checkbox" onclick="clearAllCBox()" />
  Clear all checkboxes
 </td><td>
  <input type="checkbox" onclick="checkAll(this,'CBoxClass')" />
  Check all CBox Class
 </td></tr>
</table>
</body>
</html>


:bananaman:
Was This Post Helpful? 1
  • +
  • -

#4 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Check All Class

Posted 09 August 2011 - 01:05 PM

Haha yeah, after running through the error console I figured out that I was using commas instead of semicolons in my for loop -.- such a simple mistake...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1