Connecting Tech Pros Worldwide Help | Site Map

Checkboxes problem !!

Jay
Guest
 
Posts: n/a
#1: Jul 23 '05
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
mscir
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Checkboxes problem !!


Jay wrote:[color=blue]
> I have a problem with javascript and checkboxes ![/color]
<snip>
[color=blue]
> <script language="Javascript"> use: <script type="text/javascript">[/color]

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

Robert
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Checkboxes problem !!


In article <5668e569.0405031849.6adaa70c@posting.google.com >,
artlover70@yahoo.com (Jay) wrote:
[color=blue]
> 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 ).[/color]

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>
mscir
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Checkboxes problem !!


Robert wrote:[color=blue]
> artlover70@yahoo.com (Jay) wrote:[color=green]
>>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 ).[/color]
>
> 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.[/color]
<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

Michael Winter
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Checkboxes problem !!


On Tue, 04 May 2004 00:13:05 -0700, mscir
<mscir@access4less.com.net.org.uk> wrote:

[snip]
[color=blue]
> function isChecked( form, name ) {
> var g = form.elements[name];
> if (g.length==null) {[/color]

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.
[color=blue]
> if(g.checked)
> return true;[/color]

Or

return g.checked;
[color=blue]
> } else {
> for(var i=0; i<g.length; ++i ) {[/color]

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.
[color=blue]
> if(g[i].checked)
> return true;[/color]

I'd recommend keeping a uniform coding style and always include braces.
[color=blue]
> }
> }
> return false;
> }[/color]

Mike

--
Michael Winter
M.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
mscir
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Checkboxes problem !!


Michael Winter wrote:[color=blue]
> mscir wrote:
> [snip]
>[color=green]
>> function isChecked( form, name ) {
>> var g = form.elements[name];
>> if (g.length==null) {[/color]
>
> 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.
>[color=green]
>> if(g.checked)
>> return true;[/color]
>
> Or
>
> return g.checked;
>[color=green]
>> } else {
>> for(var i=0; i<g.length; ++i ) {[/color]
>
> 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.
>[color=green]
>> if(g[i].checked)
>> return true;[/color]
>
> I'd recommend keeping a uniform coding style and always include braces.
>[color=green]
>> }
>> }
>> return false;
>> }[/color]
>
>
> Mike[/color]

Those are some good points, thanks,
Mike

Richard Cornford
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Checkboxes problem !!


Jay wrote:
<snip>[color=blue]
> 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 ).[/color]

This exact problem is covered at:-

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

Richard.


Jay
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Checkboxes problem !!


Thank you all for your helps and good suggestions on how to improve my code !!!

Jay
Closed Thread