HighDots Forums  

Re: Checkboxes problem !!

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Checkboxes problem !! in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mscir
 
Posts: n/a

Default Re: Checkboxes problem !! - 05-04-2004 , 03:13 AM






Robert wrote:
Quote:
artlover70 (AT) yahoo (DOT) 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



Reply With Quote
  #2  
Old   
Michael Winter
 
Posts: n/a

Default Re: Checkboxes problem !! - 05-04-2004 , 07:21 AM






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

[snip]

Quote:
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.

Quote:
if(g.checked)
return true;
Or

return g.checked;

Quote:
} 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.

Quote:
if(g[i].checked)
return true;
I'd recommend keeping a uniform coding style and always include braces.

Quote:
}
}
return false;
}
Mike

--
Michael Winter
M.Winter (AT) blueyonder (DOT) co.invalid (replace ".invalid" with ".uk" to reply)


Reply With Quote
  #3  
Old   
mscir
 
Posts: n/a

Default Re: Checkboxes problem !! - 05-04-2004 , 07:32 AM



Michael Winter wrote:
Quote:
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



Reply With Quote
Reply




Thread Tools
Display Modes

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.