Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

object.length undefined.

Status
Not open for further replies.

rxsid

Programmer
Jul 9, 2002
57
US
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 = manager.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?
 
typo in above...should be:


var elemLength = form1.listing.length;
 
More info:
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:
echo "<input type=checkbox name=listing value="$someID">";

HTML:
<input type="button" value="Do Check" onClick="return doCheck();">

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).
 
found the answer:

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.checked) {
//do some work here.
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top