views:

101

answers:

1

Hi all

Trying an example piece of code for scriptaculous for doing some drag and drop. It works fine in IE8 but Firefox and Chrome generate an error of 'sections.each is not a function'

Here is the code:

function getGroupOrder() {
    var sections = document.getElementsByClassName('section');
    var alerttext = '';
    sections.each(function(section) {
        var sectionID = section.id;
        var order = Sortable.serialize(sectionID);
        var mySectionID = Right(section.id);
        var myLen = String(Sortable.sequence(section)).length;
        var StuCode = "";
        if (myLen ==8)
        {var StuCode = String(Sortable.sequence(section)).substring(myLen, 2);}
        else if (myLen ==9)
        {var StuCode = String(Sortable.sequence(section)).substring(myLen, 3);}

        alerttext += mySectionID + ': ' + StuCode + '\n';
            alerttextb = sectionID + ': ' + StuCode + '\n';
    }
}

One solution suggested on a forum "I was able to resolve this issue by wrapping the call to document.getElementsByClassName('section'); with $A()" but I don't have a clue what that means! I asked what it meant but the post was made in 2008 and no reply as yet.

Thanks for any help provided.

Regards

+2  A: 

The getElementsByClassName method on native implementations returns a NodeList, not an Array object.

The $A method from PrototypeJS to converts any iterable object into an Array object, for example:

$A(sections).each(function(section) {
  //...
});
CMS
Looking at the docs, I see that the [`getElementsByClassName`](http://www.prototypejs.org/api/element/getElementsByClassName) method has been deprecated in Prototype 1.6, they now suggest the use of [`$$`](http://www.prototypejs.org/api/utility/dollar-dollar) or [`Element#select`](http://www.prototypejs.org/api/element/select) for the fact I stated, native implementations of `getElementsByClassName` return `NodeList` objects...
CMS
Thanks for that, ill keep an eye out if I update.
tonyyeb