473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Javascript select all option

1
Hi,
I am new to javascript. I want to select all the checkboxes or deselect all in a form, based on the state of one checkbox. Here is the code:

[HTML]<html>

<script language="JavaScript">
function toggleChecked(oElement) {
oForm = oElement.form;
oElement = oForm.elements[oElement.name];
alert("oElement :"+ oElement.length);
if(oElement.length) {
bChecked = oElement[0].checked;
for(i = 1; i < oElement.length; i++)
oElement[i].checked = bChecked;
}
}
</script>

<form action="javascript:" onsubmit="return false">
<INPUT TYPE=CHECKBOX NAME="checkall" value="checkall" onClick="toggleChecked(this)"/>Check all<BR>
<INPUT TYPE=CHECKBOX NAME="mushrooms" value="mushrooms">mushrooms<BR>
<INPUT TYPE=CHECKBOX NAME="greenpeppers" value="greenpeppers">green peppers<BR>
<INPUT TYPE=CHECKBOX NAME="olives" value="olives">olives<BR>
<INPUT TYPE=CHECKBOX NAME="onions" value=""onions>onions<P>

<INPUT TYPE=SUBMIT VALUE="submit">

</form>
</html>[/HTML]

But I am getting the form value as null and no length inside the javascript.

Please help. Thanks in advance.
Feb 25 '07 #1
4 3509
dmjpro
2,476 2GB
look let's know a secret of js

<input name = x>
<input name = y>

if u try to write ...
x.length then it is undefined
because in DOM there is only one element by name x

just to solve ur problem try to give the same name to ur all checkboxes ...

now try this code urself ....best of luck

welcome in advance
Feb 26 '07 #2
[HTML]
<script type="text/javascript">
window.onload=function()
{
var chb=document.getElementsByTagName("input");
for(var i=0;i<chb.length;i++)
{
if(chb[i].type.toString().toLowerCase()=="checkbox")
{
chb[i].checked=true;
}
}
}
</script>
[/HTML]
Feb 26 '07 #3
acoder
16,027 Expert Mod 8TB
[HTML]
<script type="text/javascript">
window.onload=function()
{
var chb=document.getElementsByTagName("input");
for(var i=0;i<chb.length;i++)
{
if(chb[i].type.toString().toLowerCase()=="checkbox")
{
chb[i].checked=true;
}
}
}
</script>
[/HTML]
This is fine, but if there are more than one set of checkboxes, it would not work as required.
Feb 27 '07 #4
acoder
16,027 Expert Mod 8TB
Hi,
I am new to javascript. I want to select all the checkboxes or deselect all in a form, based on the state of one checkbox. Here is the code:

[HTML]<html>

<script language="JavaScript">
function toggleChecked(oElement) {
oForm = oElement.form;
oElement = oForm.elements[oElement.name];
alert("oElement :"+ oElement.length);
if(oElement.length) {
bChecked = oElement[0].checked;
for(i = 1; i < oElement.length; i++)
oElement[i].checked = bChecked;
}
}
</script>

<form action="javascript:" onsubmit="return false">
<INPUT TYPE=CHECKBOX NAME="checkall" value="checkall" onClick="toggleChecked(this)"/>Check all<BR>
<INPUT TYPE=CHECKBOX NAME="mushrooms" value="mushrooms">mushrooms<BR>
<INPUT TYPE=CHECKBOX NAME="greenpeppers" value="greenpeppers">green peppers<BR>
<INPUT TYPE=CHECKBOX NAME="olives" value="olives">olives<BR>
<INPUT TYPE=CHECKBOX NAME="onions" value=""onions>onions<P>

<INPUT TYPE=SUBMIT VALUE="submit">

</form>
</html>[/HTML]

But I am getting the form value as null and no length inside the javascript.

Please help. Thanks in advance.
As dmjpro pointed out, you need to use the same name for each group of checkboxes.

You should define a name and id for your form. Also, pass the checkbox group to the function too. "this" is the particular checkbox that the onclick event takes place (the toggle checkbox).

Try something like the following after those changes:
Expand|Select|Wrap|Line Numbers
  1. function toggleChecked(checkboxGrp,oElement) {
  2.  bChecked = oElement.checked;
  3.  for(i = 0; i < checkboxGrp.length; i++)
  4.  checkboxGrp[i].checked = bChecked;
  5. }
Feb 27 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: bin_P19 P | last post by:
the code i have got is as follows and now im stuck <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Shopping...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
3
by: Richard | last post by:
I have tried to create a javascript puzzle where by the grid is filled with numbers from 1 to 26 and another grid listed 1 to 26 where each number relates to a letter. Once you figure out the...
9
by: beguigne | last post by:
Below is a snippet of a crude date picking routine for a form. I am a novice at this so, I am hitting my head at why when I select the day, the onChange event gives me a blank. What am I missing?...
7
by: C.Joseph Drayton | last post by:
I have a problem that I am hoping someone can help me with. First let me describe the problem. I have an HTML form that in one field has an onBlur call to a JavaScript function. When you exit the...
13
by: monomaniac21 | last post by:
hi i want to be able to trigger a javascript style popup alert in php (i want a message displayed on the actual page) is this possible?
3
by: bhanubalaji | last post by:
hi, I am unable to disable the text(label) in javascript..it's working fine with IE,but i am using MOZILLA.. can any one help regarding this.. What's the wrong with my code? I am...
4
by: JLupear | last post by:
My friend and I are trying to start a business and are writing a website of our own. We have been trying to create an online estimator and are having trouble with writing the javascript that is to...
3
by: safiratec | last post by:
Hi, I want to show a div depending of the value of a <select> option, using 2 functions hidediv() and showdiv() - it is working fine in firefox, but not in IE (tested with IE 6 and 7). <body...
3
by: Venturini | last post by:
I am trying to put together a web page where the customer makes choices of products and is then given a total. I am extremely new to Javascript and have managed to get as far as I have from web...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.