JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 28th, 2004, 07:22 PM
rxsid rxsid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 238 rxsid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 18 h 49 m 56 sec
Reputation Power: 10
Question object.length undefined

Hi all,

I've got a form (called form1) and a checkbox element (called listing).

When my js function is executed on click, the following elemLength displays 2 if 2 of the listing checkboxes are checked, or 5 if 5 are checked, etc.

var elemLength = form1.listing.length;

However...if only 1 listing checkbox is available to check and it's checked, elemLength returns undefined. Why? Why wouldn't it return 1?

Reply With Quote
  #2  
Old June 28th, 2004, 07:28 PM
jacktasia jacktasia is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559 jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 14 h 10 m 18 sec
Reputation Power: 12
Send a message via AIM to jacktasia
post the code or a link to the page please.

Reply With Quote
  #3  
Old June 28th, 2004, 07:37 PM
rxsid rxsid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 238 rxsid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 18 h 49 m 56 sec
Reputation Power: 10
The listing checkbox is generated via a php db query and can be 1 or more checkboxes....all named listing with some different value.

[javascript]
function doCheck() {
var elemLength = form1.listing.length;
alert(elemLength);
}
[/javascript]
PHP Code:
echo "<input type=checkbox name=listing value="$someID">"


[html]
<input type="button" value="Do Check" onClick="return doCheck();">
[/html]

if only 1 checkbox is generated, and it is checked...the elemLength var is undefined....even though there is one .listing that is checked. I can do an alert (form1.listing.value) and it does show the $someID.
if 2+ checkboxes are generated, and 2+ of them are checked...elemLength show's it's length (ie how many are checked).

Reply With Quote
  #4  
Old June 28th, 2004, 08:02 PM
jacktasia jacktasia is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559 jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 14 h 10 m 18 sec
Reputation Power: 12
Send a message via AIM to jacktasia
i think it's because the object hasn't been turned into an array unless there is more than one, so .length isn't of vaild usage...why can't you just test and see if it's undefined and if it is then you know it's one, like this:

Code:
var elemLength =document.form1.listing.length;
if (elemLength==undefined) {
	elemLength=1;
}

Last edited by jacktasia : June 28th, 2004 at 08:07 PM.

Reply With Quote
  #5  
Old June 28th, 2004, 08:07 PM
rxsid rxsid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 238 rxsid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 18 h 49 m 56 sec
Reputation Power: 10
that will check for undefined, but here's what i have later in the code that doesn't work:

Code:
for (var i = 0; i<elemLength; i++) {
    if (form1.listing[i].checked) {
        //do some work here.   
    }
}

if the 1 checkbox scenario is present, the above code give me a run time error.

Reply With Quote
  #6  
Old June 28th, 2004, 08:13 PM
jacktasia jacktasia is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: Lawrence, Kansas [KU]
Posts: 1,559 jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level)jacktasia User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 14 h 10 m 18 sec
Reputation Power: 12
Send a message via AIM to jacktasia
well, you of course would have to do something like this...

Code:
if (elemLength==undefined) {
	elemLength=1;
	if (document.form1.listing.checked) {
		// we know the one and only is checked
	}
} else {
	for (var i = 0; i<elemLength; i++) {
		if (form1.listing[i].checked) {
			//do some work here.
		}
	}
}

Reply With Quote
  #7  
Old June 28th, 2004, 08:20 PM
rxsid rxsid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 238 rxsid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 18 h 49 m 56 sec
Reputation Power: 10
bingo!

that did the trick.

thanks jacktasia!!

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > object.length undefined


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap