[thelist] javascript select forms

Matt Warden mwarden at gmail.com
Wed Jul 13 01:08:17 CDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bernardo Escalona-Espinosa wrote:
> If anyone knows of a script that does something similar please let me
> know. I dont need the exact code, but just a place where I can get
> some ideas on how I can achieve this with elegance and simplicity.

I think it would be simplest to think about things with an ordered
array. This assumes that the possible order positions is equal to the
number of photos (makes sense). This is also untested. It's just to
convey what I'm thinking.

var photos = new Array('a', 'c', 'd', 'e', 'i', 'y');

function refreshOrder()
{
   for (var i=0; i<photos.length; i++)
   {
      // get reference to select box with id 'imagename_order'
      var sel = document.getElementById(photos[i] + '_order');

      // assign the selected number of this select box equal
      // to the index of the photo in the ordered array. If
      // you want to use 1-based order positions rather than
      // 0-based, you'll ahve to adjust this.
      sel.selectedIndex = i;
   } // end for

} // end function


function setOrder(photo, index)
{
   // you could probably do better with this function by using
   // a published algorithm

   // remove and shift down
   for (var i=0; i<photos.length; i++)
   {
      if (photos[i] == photo)
      {
         // you could just as easily make this happen in the
         // outer loop, but it's a little easier to follow this way
         for (var j=i; j<photos.length-1; j++)
         {
            // shift down
            photos[j] = photos[j+1];
         } // end if

         // put our photo at the end
         photos[photos.length-1] = photo;

         // bubble it down until index
         for (var k=photos.length-1; k>index; k--)
         {
            // swap k and k-1
            var old = photos[k-1];
            photos[k-1] = photos[k];
            photos[k] = old;
         }
         break;
      } // end if found
   } // end outer for

} // end function


Ok. Maybe that isn't easiest. I'll let you decide.


- --
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFC1K/RAQ0d4HGyPE8RAkafAJwKO1zrk76wUhnrbafGCGWVQ9dCNQCcDpfl
bS0ad2UbAr7nlVtLTQetiqM=
=NrR8
-----END PGP SIGNATURE-----


More information about the thelist mailing list