Arguments Accepted

by gaby de wilde

HTML

<div id="foo" > this is the content of div "foo" </div>
<div id="foo2"> this is the content of div "foo2" </div>
<pre>

   <h3>accepts element id</h3>
    
    myLibrary('foo')
    
   <h3> accepts objects</h3>

    myObject = document.getElementById('myElement');
    myLibrary( myObject )
    
    <h3>accepts arrays of id's</h3>
    
    idArray = [ 'foo' , 'foo2' ];
    myLibrary( idArray )
    
    <h3>accepts mixed arrays</h3>

    myObject = document.getElementById('myElement');
    idArray = [ 'foo' , 'foo2' , myObject ];
    myLibrary( idArray );
    
    <h3>accepts multiple arguments as id's</h3>
    
    myLibrary( 'foo' , 'foo2' )
    
    <h3>accepts multiple arguments as objects</h3>

    myObject = document.getElementById('myElement');
    myLibrary( myObject );
    
    <h3>accepts mixed arguments</h3>

    myObject = document.getElementById('myElement');
    myLibrary( 'foo' , 'foo2' , myObject  )
    
        <h3>Not supported</h3>
    
    If multiple arrays are provided as arguments only the first one is used.
    Arrays with arrays dont work.
</pre>

CSS

h3{ color:red;background:yellow; }
body { background:#331; color:#fff; }

JavaScript

myLibrary = function (e) {
    
    /* accepts dom referrences in the form of:
        objects, 
        id (as a string),
        array of id,
        arrays of objects,
        arrays of id's mixed with objects,
        multiple arguments (as if an array),
        */
    
    e = ( arguments.length > 1 ) ? [].slice.call(arguments) : (Array.isArray(e)) ? e : [e];   
    for ( a in e ){ myObj = (typeof e[a] === "string") ? document.getElementById(e[a]) : e[a];
          
        /* do your something with myObj */
    
        alert(myObj.innerHTML);
        

    }
}

myLibrary('foo','foo2');