Forum Moderators: open

Message Too Old, No Replies

Array values to reflect list order

This is advanced..

         

Fiasst

6:27 pm on Sep 7, 2007 (gmt 0)

10+ Year Member



Hi,

I'm really stuck with an array I'm trying to convert.

The array looks like:

liOrder=new Array('li0', 'li1', 'li2', 'li3');

The order of the list items on my page can change. Therefor the order of the liOrder array can also change.
For example: liOrder=('li2', 'li1', 'li0', 'li3');

I need to first check if the order of the array has changed (if the list items have been dragged and re-ordered by the user).


<script language="JavaScript" type="text/javascript">
if(liOrder!="0,1,2,3"){

// if the list has been re-ordered I need a way to figure out
// what the order is on page load and somehow, re-order the
// list items to the liOrder arrays new value.

}
</script>

I don't know howto do this! I'm hoping someone here does :)

I should state, I've written the javascript to create and read a cookie to save the order of the list items. I just can't figure out how to change the list structure to reflect the order.

Thanks for reading. Sorry about the lengthy post!

Fotiman

6:45 pm on Sep 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why not store, in the cookie, the array as a comma delimited list:

"'li2','li1','li0','li3'"

(looks like you're instead just storing it as "2,1,0,3").

Next, just eval that into an array:

var arr = eval("[" + yourcookiestring + "]");
// eval("['li2','li1','li0','li3']")

Little_G

6:55 pm on Sep 7, 2007 (gmt 0)

10+ Year Member



..and then to write the list, something like:


[pre]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>List equal to Array</title>
<script type="text/javascript">
window.onload = function(){
var testOrder = "'li2','li1','li0','li3'";
var arr = eval("[" + testOrder + "]");
var lista = document.getElementById('lista');
lista.innerHTML = "<li>" + arr.join("</li><li>") + "</li>";
}
</script>
</head>
<body>
<ul id="lista">
<li>li1</li>
<li>li2</li>
<li>li3</li>
<li>li4</li>
</ul>
</body>
</html>[/pre]

Andrew

[edited by: Little_G at 6:56 pm (utc) on Sep. 7, 2007]

Fiasst

7:12 pm on Sep 7, 2007 (gmt 0)

10+ Year Member



Hi, thanks for replying.

Sorry I wasn't clear enough. the list items contain a lot of content (all unique to that list item). This isn't a case of creating a specifically ordered list. I need to alter the order of the existing lists items and their child content.

Cheers,
Marc

Fotiman

7:34 pm on Sep 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, relying on JavaScript to control the order of your content is probably not a good idea. You should instead probably be writing the order to a database, and then server side you should be checking that order to determine how to output the content.