JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 2nd, 2006, 03:34 PM
nevillemehta nevillemehta is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: India
Posts: 64 nevillemehta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 44 m 17 sec
Reputation Power: 16
Access chechbox array...

Hi,

Please have a look at this code...

Code:
<html>
<head>
<script lang='JavaScript'>
function toggleTick(count)
{
	if (document.former.cbx_foo[count].checked == true)
		document.former.cbx_foo[count].checked = false;
	else
		document.former.cbx_foo[count].checked = true;
}
function checkAll(count)
{
	for (var j = 1; j <= count; j++)
	{
		if (document.former.cbx_foo[count].checked == false) document.former.cbx_foo[count].checked = true;
   	}
}
</script>
</head>
<body>
<form name="former">
<input type='checkbox' name='cbx_foo[]' value='0'><a href="#" onmouseover=toggleTick(0)>Text</a>
<br>
<input type='checkbox' name='cbx_foo[]' value='0'><a href="#" onmouseover=toggleTick(1)>Text</a>
<br>
<input type='checkbox' name='cbx_foo[]' value='0'><a href="#" onmouseover=toggleTick(2)>Text</a>
<br>
<input type="button" value="Check All" onClick="checkAll(3)">
</form>
</body>
</html>


The lines where i try to access any checkbox (e.g. document.former.cbx_foo[count].checked) are giving errors. Can someone help me with the correct line? For this reason niether the toggleTick nor the checkAll function is working properly.

Thanks

Reply With Quote
  #2  
Old June 2nd, 2006, 09:51 PM
Mittineague's Avatar
Mittineague Mittineague is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 552 Mittineague User rank is Private First Class (20 - 50 Reputation Level)Mittineague User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 1 m 33 sec
Reputation Power: 13
checkboxes

You're close.
Although checkboxes are an array, the names are not. Remove the [] from the names and the first function will work.
The second function has a few problems. It's OK to pass "count" to it and use that to limit the for loop incrementing. Arrays start at "0" not 1, change this to 0 if you want the first checkbox to be included in the "check all".
The less "than or equal to" will increment to the "count" value. This is 1 more than the index values of the checkbox array, because they start at 0. Lastly, you should use the incrementing value "j" to id the checkboxes inside the loop, not "count".

Last edited by Mittineague : June 2nd, 2006 at 10:04 PM.

Reply With Quote
  #3  
Old June 2nd, 2006, 11:40 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 140 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 33 m 14 sec
Reputation Power: 13
Code:
Although checkboxes are an array, the names are not. Remove the [] from the names and the first function will work.

No need. JavaScript doesn't recognize the names as an array, so you have to modify your code accordingly.

Change all instances of
Code:
document.former.cbx_foo[count].checked
to
Code:
document.former.elements['cbx_foo[]'][count].checked

Reply With Quote
  #4  
Old June 5th, 2006, 04:15 AM
ravs ravs is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Location: gurgaon, haryana, india
Posts: 60 ravs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 29 sec
Reputation Power: 12
Send a message via Yahoo to ravs
hey

Kravvitz is right

but u can use this thig one more way

Code:
var oChks= document.getElementsByName("CheckBoxName")

for(i=0;i<oChks.length;i++)
{
        if(oChks[i].checked==true)
         {
           // do what ever you want
         }
}


but before using these thing learn the diffrence between NAME and ID (not only these two but the very basics of HTML, i hope you know but know it with javascript). it will give you great help in developing thing and will giveyou good understanding of javascript,

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Access chechbox array...


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap