Copyright ©1997-2009 Glenn Fleishman except as noted otherwise. All rights reserved. For permission to reprint, contact Glenn Fleishman at glenn at glennf.com. Photo © 2008 Laurence Chen; used with permission.
Turning technology from mumbo-jumbo into rich tasty gumbo
< Someone Finally Calls BS on Amazon's Highest Unit Sales PR | Main | Test the New Isbn.nu >This is very wonky, but I need to put it out on the internets, all of them, because it took me a good 20 minutes to figure this out.
In revising isbn.nu (new version will soon be in beta for your viewing and testing pleasure), I have added a bookmarks option where you set up an account, and can mark books, authors, or works for later recollection. I wanted to let people re-order the items in their bookmark lists at will. And I wanted this to be live -- JavaScript (AJAX for the database backend), not a page reload.
So I wrote up the code and it worked quite well, but the swapping seemed arbitrary after a couple of moves. Then I realized: I was using ID fields to swap items live, and I wasn't changing the ID of a particular row after I changed it. So, once swapped, it still had the old ID number but the new contents.
Enter getElementsByTag, which lets you rename the ID of a given object in the DOM. In this case, I'm passing the swap_prefix (a name) which is combined with an underscore and an ID from a database. That defines the row. Then I swap the internals using innerHTML, and then the ID using getElementsByTagName. Behind the scenes (not shown here), all the database updating is happening.
Here's the code, in case this helps someone at a future time. It requires Prototype.js, or you have to put in document.getElementById() instead of $().
var swap1 = swap_prefix + '_' + ids[1];
var swap2 = swap_prefix + '_' + ids[2];
var swapx_text = $(swap1).innerHTML;
$(swap1).innerHTML = $(swap2).innerHTML;
$(swap2).innerHTML = swapx_text;
var swap1tag = $(swap1).getElementsByTagName('div');
var swap2tag = $(swap2).getElementsByTagName('div');
swap1tag[0].parentNode.id = swap2;
swap2tag[0].parentNode.id = swap1;
Posted by Glennf at January 20, 2009 12:09 PM
MegaGlennFeed
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
Recent Entries
Archives
July 2009 | May 2009 | April 2009 | March 2009 | February 2009 | January 2009 | December 2008 | November 2008 | October 2008 | September 2008 | August 2008 | July 2008 | June 2008 | May 2008 | April 2008 | March 2008 | February 2008 | January 2008 | December 2007 | November 2007 | October 2007 | September 2007 | August 2007 | July 2007 | June 2007 | May 2007 | April 2007 | March 2007 | February 2007 | January 2007 | December 2006 | November 2006 | October 2006 | September 2006 | August 2006 | July 2006 | June 2006 | May 2006 | April 2006 | March 2006 | February 2006 | January 2006 | December 2005 | November 2005 | October 2005 | September 2005 | August 2005 | July 2005 | June 2005 | May 2005 | April 2005 | March 2005 | February 2005 | January 2005 | December 2004 | November 2004 | October 2004 | September 2004 | August 2004 | July 2004 | June 2004 | May 2004 | April 2004 | March 2004 | February 2004 | January 2004 | December 2003 | November 2003 | October 2003 | September 2003 | August 2003 | July 2003 | June 2003 | May 2003 | April 2003 | March 2003 | February 2003 | January 2003 | December 2002 | November 2002 | October 2002 | September 2002 | August 2002 | July 2002 | June 2002 | May 2002 | April 2002 | March 2002 | February 2002 | January 2002 | December 2001 | November 2001 | October 2001 |