470,647 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

Checkboxes problem !!

Jay
Hi everybody!

I have a problem with javascript and checkboxes !

Here is a similir version of the page that I have:
=============
<html>
<head>

<script language="Javascript">
<!--
function isChecked( form, name )
{
var g = form.elements[ name ];

//alert(g.length);

for(var i = 0, n = g.length; i < n; ++i )
{
if( g[ i ].checked )
return true;
}

return false;
}

var clickedButton;
function validate( form )
{
if( !isChecked( form, "entries[]" ))
{
alert( "ERROR: No entry/entries selected !!!" );
return false;
}
// Check to see which buttons was clicked.
if (clickedButton.value == 'Delete')
{
return confirm("Are you sure you want to delete selected
entry/entries ?");
}
if (clickedButton.value == 'Edit')
{
return true;
}
return false;
}

//-->
</script>

</head>

<body>

<h3>Records</h3>

<form name='display' method='POST' onsubmit='return validate(this);'
action='dosomething.php' >
<table border=1>

<tr>
<td></td>
<th>Heading1</td>
<th>Heading2</td>
<th>Heading3</td>
</tr>

<tr>
<td><input type='checkbox' name='entries[]' value='entryID1'></td>
<td >Something</td>
<td >Something</td>
<td >Something</td>
</tr>

<tr>
<td><input type='checkbox' name='entries[]' value='entryID2'></td>
<td >Something</td>
<td >Something</td>
<td >Something</td>
</tr>

<tr>
<td><input type='checkbox' name='entries[]' value='entryID3'></td>
<td >Something</td>
<td >Something</td>
<td >Something</td>
</tr>

<tr>
<td colspan=4>
<input type='submit' name='delete' value='Delete'
onclick='clickedButton=this'>
<input type='submit' name='edit' value='Edit'
onclick='clickedButton=this'>
</td>
</tr>
</table>
</form>

</body>
</html>
=============

It works fine when there are more than one entry ( row) in the table
(like the current page). However, when there is only one entry on the
page and when I click on "Delete" or "Edit", I always get the message
"No Entry/entries selected" even though I already select that entry (
The form is supposed to be submitted ).
Any help would be appreciated !!

Thanks,

Jay
Jul 23 '05 #1
7 1533
Jay wrote:
I have a problem with javascript and checkboxes ! <snip>
<script language="Javascript"> use: <script type="text/javascript">


How about this approach: loop through all of the form elements, see if
any of them, whose names are equal to the string you passed the
function, are checked. Too, you might consider making your variable
names different from page elements or their attributes, to me it's
confusing, and I'm not sure but I think it might cause you problems with
code in some cases. I renamed entries[] to entries, I'm not sure why
you'd want brackets there.

<script type="text/javascript">
var clickedButton;
function isChecked(targetform,objName) {
var g = document.forms[targetform.name].elements;
for(var i=0; i<g.length; ++i) {
if(g[i].checked && g[i].name==objName)
return true;
}
return false;
}
function validate(targetform) {
if (!isChecked(targetform, "entries")) {
alert("ERROR: No entry/entries selected !!!");
return false;
}
if (clickedButton.value == 'Delete')
return confirm("Are you sure you want to delete selected
entry/entries ?");
if (clickedButton.value == 'Edit')
return true;
return false;
}
</script>
</script>

<input type='checkbox' name='entries' value='entryID1'>

Mike

Jul 23 '05 #2
In article <56**************************@posting.google.com >,
ar********@yahoo.com (Jay) wrote:
However, when there is only one entry on the
page and when I click on "Delete" or "Edit", I always get the message
"No Entry/entries selected" even though I already select that entry (
The form is supposed to be submitted ).


You were close to the problem with the commented out alert. When you
have only one row, you do not have an array of names, but a single
value. Perhaps there is some way of forcing a one element array of
names, but I do not know it. I added some special case code for the one
element array.

Minor changes:
- doctype
- title
- type="text/javascript" instead of the depreciated language attribute
- no need to hide javascript for Netscape release 1.

I tested this script in Netscape 7.1.

Robert
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<TITLE>One Row</TITLE>
<script type="text/javascript">

function isChecked( form, name )
{

var g = form.elements[ name ];

alert("name = " + name +
" g.length = " + g.length);

if (g.length == undefined)
{
alert("Only one row is present.");
if( g.checked )
{return true;}
}
else
{
for(var i = 0, n = g.length; i < n; ++i )
{
if( g[ i ].checked )
{return true;}
}
}
return false;
}

var clickedButton;
function validate( form )
{
if( !isChecked( form, "entries[]" ))
{
alert( "ERROR: No entry/entries selected !!!" );
return false;
}
// Check to see which buttons was clicked.
if (clickedButton.value == 'Delete')
{
return confirm("Are you sure you want to delete selected
entry/entries ?");
}
if (clickedButton.value == 'Edit')
{
return true;
}
return false;
}
</script>

</head>

<body>

<h3>Records</h3>

<form name='display' method='POST' onsubmit='return validate(this);'
action='dosomething.php' >
<table border=1>

<tr>
<td></td>
<th>Heading1</td>
<th>Heading2</td>
<th>Heading3</td>
</tr>

<tr>
<td><input type='checkbox' name='entries[]' value='entryID1'></td>
<td >Something</td>
<td >Something</td>
<td >Something</td>
</tr>

<tr>
<td colspan=4>
<input type='submit' name='delete' value='Delete'
onclick='clickedButton=this'>
<input type='submit' name='edit' value='Edit'
onclick='clickedButton=this'>
</td>
</tr>
</table>
</form>

</body>
</html>
Jul 23 '05 #3
Robert wrote:
ar********@yahoo.com (Jay) wrote:
However, when there is only one entry on the
page and when I click on "Delete" or "Edit", I always get the message
"No Entry/entries selected" even though I already select that entry (
The form is supposed to be submitted ).


You were close to the problem with the commented out alert. When you
have only one row, you do not have an array of names, but a single
value. Perhaps there is some way of forcing a one element array of
names, but I do not know it.

<snip>

How about this:

function isChecked( form, name ) {
var g = form.elements[name];
if (g.length==null) {
if(g.checked)
return true;
} else {
for(var i=0; i<g.length; ++i ) {
if(g[i].checked)
return true;
}
}
return false;
}

Mike

Jul 23 '05 #4
On Tue, 04 May 2004 00:13:05 -0700, mscir
<ms***@access4less.com.net.org.uk> wrote:

[snip]
function isChecked( form, name ) {
var g = form.elements[name];
if (g.length==null) {
Comparing to undefined or null is a bad idea. For one, undefined, as a
keyword, isn't recognised by all engines. Secondly, comparing to null is
misleading as your actually comparing to undefined. Instead, do

if( !g.length ) {

This would evaluate true if length is undefined or zero. As it can never
be zero, it'll do nicely.
if(g.checked)
return true;
Or

return g.checked;
} else {
for(var i=0; i<g.length; ++i ) {
for( var i = 0, n = g.length; i < n; ++i ) {

is faster. Actually, declaring 'n' along with 'g' at the start of the
function, then writing

if( !( n = g.length )) {

and

for( var i = 0; i < n; ++i ) {

involves the fewest lookups.
if(g[i].checked)
return true;
I'd recommend keeping a uniform coding style and always include braces.
}
}
return false;
}


Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #5
Michael Winter wrote:
mscir wrote:
[snip]
function isChecked( form, name ) {
var g = form.elements[name];
if (g.length==null) {


Comparing to undefined or null is a bad idea. For one, undefined, as a
keyword, isn't recognised by all engines. Secondly, comparing to null is
misleading as your actually comparing to undefined. Instead, do

if( !g.length ) {

This would evaluate true if length is undefined or zero. As it can never
be zero, it'll do nicely.
if(g.checked)
return true;


Or

return g.checked;
} else {
for(var i=0; i<g.length; ++i ) {


for( var i = 0, n = g.length; i < n; ++i ) {

is faster. Actually, declaring 'n' along with 'g' at the start of the
function, then writing

if( !( n = g.length )) {

and

for( var i = 0; i < n; ++i ) {

involves the fewest lookups.
if(g[i].checked)
return true;


I'd recommend keeping a uniform coding style and always include braces.
}
}
return false;
}

Mike


Those are some good points, thanks,
Mike

Jul 23 '05 #6
Jay wrote:
<snip>
It works fine when there are more than one entry ( row) in the table
(like the current page). However, when there is only one entry on the
page and when I click on "Delete" or "Edit", I always get the message
"No Entry/entries selected" even though I already select that entry (
The form is supposed to be submitted ).


This exact problem is covered at:-

<URL: http://www.jibbering.com/faq/faq_not...ess.html#faBut >

Richard.
Jul 23 '05 #7
Jay
Thank you all for your helps and good suggestions on how to improve my code !!!

Jay
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ralph Freshour | last post: by
5 posts views Thread by @(none) | last post: by
6 posts views Thread by terence.parker | last post: by
8 posts views Thread by PeteOlcott | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.