Forum Moderators: open

Message Too Old, No Replies

Checking if an array

         

Nutter

9:13 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



I have a pre-submit routine on a page that makes sure at least one of a list of checkboxes is filled in. It can be more than one, but at least one must be checked. It works great when there's several possible items in the list - I step through the array and if I find one that's checked then I go ahead and submit. Problem is if there is only one member. .length becomes undefined and I cannot check it. Is there an is_array() type function for JS that I could use here?

Fotiman

11:00 pm on Mar 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could write your own:

function isArray( arr )
{
if( "undefined" == typeof arr[0] )
alert("no");
else
alert("yes");
}
var x = "Foo";
var y = new Array("A","B","C");
isArray(x);
isArray(y);

Bernard Marx

12:06 am on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



eg: formRef.elements.aName

The DOM 0 referencing methods, as you have found, return an element if there is only one, and a collection if more:

Is there an is_array() type function for JS that I could use here?

Could just ask:

if( obj.constructor==Array)

..but you can't use that here, because what is returned (when multiple) is a collection not a native Javascript array.

Testing for obj[0] is a reasonable alternative is this case.

You could "normalise" as you step through:

if(!obj[0]) obj=[obj];

Now, you can carry on, treating every reference you get as a collection. Even though some are native arrays, it will all be the same for our purposes.

ALTERNATIVELY

You could use DOM 1 to do the initial referencing

// from this
var boxes = document.forms.myForm.checks;
if(!boxes[0]) boxes = [boxes]; // wrap in array, if single

// to this
var boxes = document.getElementsByName("checks");

The DOM 1 collection methods always return a collection. This is even better, since it means that you can run your loop, even if there are no boxes at all (The first method requires an extra line for that)

prasanth jvrs

7:32 am on Mar 16, 2006 (gmt 0)

10+ Year Member



Hi,

Hope this code helps you.

<script>

var radiogroup=document.forms[0].portlet1;

// Code if many are there
for(var j = 0 ; j <radiogroup.length;j++) {

if(radiogroup[j].checked) {
itemchecked = true;
empIdVal=radiogroup[j].value;
// alert(empIdVal);
break;
}
}

// Code if one is there.....

if(radiogroup!= null && radiogroup.length == null){
//alert("came");
if(radiogroup.checked) {
itemchecked = true;
empIdVal=radiogroup.value;
// alert(empIdVal);
}
}
</script>

Nutter

5:36 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



Thanks all. I wound up using a mix of everything. I set var x = document.....checkboxcontrol. Then did then if (!x[0]) x[0] = document....checkboxcontrol. And, I've got a single member array. Works like a charm (so far :-) )

Bernard Marx

7:45 pm on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a single member array

You've added the checkbox to itself, as it's 0th member. Interesting. Be careful though - it's not an array.

Nutter

8:20 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



Can you explain why it's not an array, and more importantly why I need to be careful with this?

Fotiman

10:51 pm on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's technically an Object, not an Array. Thus, if you attempt to do:

alert(x.length);

After you do your assignment to x[0], you will still get undefined.

But you could probably modify it like so:

if (!x[0])
x = new Array( x );

Bernard Marx

12:28 am on Mar 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var x = document.blah.checkboxcontrol
if (!x[0]) x[0] = document.blah.checkboxcontrol

Firstly, this would have been easier (since x is already pointing there):

if(!x[0]) x[0]=x;

Perhaps it's clearer now.


Native Javascript of objects are "soft" - you can add or remove "members" at will.
The majority of the other objects you're likely to meet in browser scripting are at least "mushy". You can add "expando" properties to DOM elements (since v4 browsers).

(There is essentially no difference at all between members associated using numbers and members associated using strings, except that you can't associate by numbers using dot syntax)

var elm = document.getElementById("myelm");
elm[35] = "hello";
elm.cupOfTea = 42;


x[0] = x;

x is now a member of itself.
x is not an array (although arrays aren't that special)

I would either wrap the element in an array:

if(!x[0]) x=[x];

or avoid the whole problem entirely:

var x = document.getElementsByName("checkboxcontrol");