0

How exactly do you check if the checkbox value you are getting is a variable or an array. I have a PHP script which dynamically populates a form with checkboxes based on the number of directories present in a folder. Now as long as there is more than one directory I can use the following code

for (i=0;i<document.getElementById('editarticle').currentcategories.length;i++)
{
    if      
(document.getElementById('editarticle').currentcategories[i].checked==true)
    {

    currentcheckedcategories[currentcheckedcategoriescounter] =  
document.getElementById('editarticle').currentcategories[i].value;

    currentcheckedcategoriescounter++



    }
    else
    {
    currentnotcheckedcategories[currentnotcheckedcategoriescounter] =    
document.getElementById('editarticle').currentcategories[i].value;
    currentnotcheckedcategoriescounter++
    }

}

However, if current-categories is not an array, i.e there is only one category, then I get an undefined and the category value is not collected. Is the only way to bypass this problem by checking if currentcategories is an array prior to running the for loop? If so how would I check if the currentcategories is an array. I read on another forum that you could use instance of Array but that doesn't seem to work and another website said that doing so would not be fail safe as it would fail one was using iFrames. Hope someone can shed light on how to check if currentcategories is an array or offer alternative code for my problem. Thanks in advance.

PS. I need the code to be in JavaScript and not JQuery or any other framework.

Partially Solved

Well, so far I have used the following if statement to check if the multiple checkboxes with the same name are present or not.

if(document.getElementById('editarticle').currentcategories.length == undefined)

An undefined means that only one checkbox with that name is available. Otherwise their are multiple currentcategories checkboxes which can be selected. Seems to work in IE 8 Firefox and Chrome. Not convinced it is the best method. Perhaps if someone sees a flaw in this implementation or a better solution please do let me know.

I tried Edorkas code but the that code returns true for array based on currentcategories values. Hence, if I have three checkboxes named currentcategories and only one is selected then the code would return false for array. Of course this is probably my fault as I worded the title of this question as "How to check if checkbox value is array" perhaps a better title would be "How to check if multiple checkboxes with same name present." I have changed that to better reflect my question.

1 Answers1

0

If the object has the attribute length defined it should be an array. Be careful about the comparison because if (result.length) will be false when the array is empty. However this function will be much more useful, found in this answer Check if object is array?

window.isArray = function(candidate){
    if (candidate == null)
        return false;
    return ( Object.prototype.toString.call(candidate) === '[object Array]' )
}
Community
  • 1
  • 1
Edorka
  • 1,781
  • 12
  • 24