Forum Moderators: open

Message Too Old, No Replies

I am having a problem with my checkbox using javascript.

Checking one check box does not work

         

sysflow

5:52 am on Mar 11, 2005 (gmt 0)

10+ Year Member



Hello,

I am having a problem with my checkbox using javascript. If anyone sees the problem, please let me know.

Everything works great if more than one checkbox is checked. In other words, checking two checkboxes sets the "BoxChecked" variable to true (this is what I want).

However, if I check only one checkbox, the "BoxChecked" variable is set to false (this is not want I want) - I want it to be set to true.

In other words, if any checkbox is checked (regardless it's one or more), BoxChecked should be set to true.

Note: The name of my form is called "myform" and the name of my checkbox elements is called "check".

function confirmDelete()
{
var BoxChecked = false;
for (var i=0; i < document.myform.check.length; i++)
{
if (document.myform.check[i].checked)
{
BoxChecked = true;
break;
}
}


if (BoxChecked == true)
{
var agree=confirm("Are you sure?");

if (agree) return true ;
else return false ;
}
if (BoxChecked == false)
{
alert("Please check a box.");
}
}

thanks in advance!

Bernard Marx

8:26 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm gonna try without the variable:

function confirmDelete() 
{
var checks = document.myform.check;
for (var i=0; i < checks.length; i++)
if (checks[i].checked)
return confirm("Are you sure?");
/* default */
alert("Please check a box.");
return false; /* best to return explicit false to stop submission */
}

sysflow

4:23 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



Still does not work. Although I like your code better since it's more compact. It is behaving the same even with your code.

Just to give a little more background, my checkboxes are created dynamically, so that any given time there can be one or more checkboxes, depending on what's in the database at the time.

I just noticed that everything works ok, when more than one checkbox is shown on my form (two or more checkboxes).

However, when only one checkbox is shown on the form and I select it, the code will default to alert("Please check a box."); - even though it's checked.

Again, if the form had two checkboxes shown, and I select only one, it works fine which I get "return confirm("Are you sure?");".

Any other suggestions?

sysflow

5:59 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



I eliminated the database part - no dynamic checkboxes. I am currently just working with just plain HTML - still same problem.

Bernard Marx

7:00 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm pretty sure, in that case, that the answer lies somewhere in the HTML, rather than the code. Can you post the form?

sysflow

7:49 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



Here it is. I took about some html overhead for clarity sake.

<html>
<head>
<title>None</title>
<link href="MAINSTYLE.CSS" rel="stylesheet" type="text/css">
</head>
<body>
<div align="center">

<script LANGUAGE="JavaScript">
<!--
function confirmDelete()
{

var checks = document.myform.check;
for (var i=0; i < checks.length; i++)
if (checks[i].checked)
return confirm("Are you sure?");
/* default */
alert("Please check a box.");
return false; /* best to return explicit false to stop submission */
}

// -->
</script>

<form name="myform" action="/links/prMain.php" method="post">
<table>
<tr>
<td>
<table>
<tr>
<td>
<input type="image" SRC="images/modify.gif" name="Modify" title="Modify Link" onClick="return confirmDelete()">
<input type="image" SRC="images/delete.gif" name="Delete" title="Delete Link" onClick="return confirmDelete()"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>
<table>
<tr>
<td>&nbsp;<INPUT TYPE=CHECKBOX id="check" NAME="linkarray[]" VALUE=33></td>
<td>&nbsp;<a href=http://dsad.com title=http://dsad.com target="_blank">dsad</a></td>
<td>&nbsp;dsad</td>
<td>&nbsp;test777</td>
<td>&nbsp;2005-03-11</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

Bernard Marx

8:13 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's only one checkbox there, but..

The checkbox's name isn't 'check' - that's it's id.
The name is 'linkarray[]'.

Change the first line of the function to:

var checks = document.myform['linkarray[]'];

sysflow

9:21 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



I already tried this and it did not work. As a matter of fact, nothing works when I use the linkarray[] - it does not get recognized by the Javascript.

I think it's because of the square brackets [] in linkarray[]. I tried it again using different variations and still does not work. Weird?

I read somewhere that you can use the id to reference a checkbox. Which seemed to have worked, except when only one check box exist in my form.

Any other suggestions?

sysflow

3:52 am on Mar 12, 2005 (gmt 0)

10+ Year Member



I dug a little deeper and referred to [us4.php.net...] which provided information on how to reference the name attribute directly, as opposed to the id attribute. So my code looks like this now:

function confirmDelete()
{
var checks = document.myform.elements['linkarray[]'];
for (var i=0; i < checks.length; i++)
if (checks[i].checked)
return confirm("Are you sure?");
/* default */
alert("Please check a box.");
return false; /* best to return explicit false to stop submission */
}

My link to now looks like <INPUT TYPE=CHECKBOX NAME="linkarray[]" VALUE=33></td>, not <INPUT TYPE=CHECKBOX id="check" NAME="linkarray[]" VALUE=33></td> as before.

Everything still works the same.

Bernard Marx

9:52 am on Mar 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Some PHP scripts require the use of [] in the name of form elements (for PHP arrays, as you know), so this comes up regularly at this forum. I gave you the standard answer.

However, you are referencing form elements via the elements collection, instead of directly off the form. This is in fact slightly more 'correct'. I didn't introduce this so as not to add confusion. In my tests, though, and as usual, the script works exactly the same way when 'elements' is left out, like I did.

It's worth noting that the script won't work when there is only one such element, because a reference to the element itself is returned, not a collection.

The truly up-to-date way to do this would be to use getElementsByName('linkarray[]'). This can't be used on the form, but I guess there's no other elements on the page called that.

document.getElementsByName('linkarray[]')

The difference is that a collection is returned, even if there is only one element.

This way leaves out version 4 browsers, while the old-fashioned way we are using works across the board.

sysflow

6:07 pm on Mar 12, 2005 (gmt 0)

10+ Year Member



THANK YOU! It works fine now, even if the user has to use a later browser that's OK.

I spent a few days trying to figure this out. Without your help, I think it might have been a lot longer, if ever, at my pace. Thanks again.