wac
11-10-2004, 07:44 PM
I would like to determine the position of an LI in an ordered list. There's the 'value' attribute, but that's unfortunately deprecated. Is there any other way???
|
||||
determine position of an ordered list itemwac 11-10-2004, 07:44 PM I would like to determine the position of an LI in an ordered list. There's the 'value' attribute, but that's unfortunately deprecated. Is there any other way??? Roy Sinclair 11-10-2004, 09:20 PM Position relative to other LI items? Position relative to the browser window? Sorry but you're not coming in clear yet. wac 11-12-2004, 04:19 PM sorry, The index value that gets displayed when one writes <ol> <li> something</li> <li> anotherthing</li> </ol> should display as 1. something 2. anotherthing If I programatically insert an LI between 1 and 2, I expected 1. something 2. inserted 3. anotherthing but I seem to always see li.value as 0 Garadon 11-12-2004, 05:19 PM I am sure this could be done simpler wouldn't be surprised, but the following code can be used to return the index of an element 0 indexed. <script> function index(){ var child=this.previousSibling; i=0; while(child!=null){child=child.previousSibling;i++} return i; } function test(){ ele=document.getElementById('t'); ele.index=index; alert(ele.index()); } Object.prototype.index=index; </script> <body onload="test();"> <ol> <li>tt</li> <li id="t">tt</li> <li>tt</li> <li>tt</li> </ol> <img> <a></a> </body> thanks to firefox I had to change the index function to this function index(){ var child=this; i=0; while(child!=null){ child=child.previousSibling;i++; if(child==null)break; while(child.nodeName=='#text'){child=child.previousSibling;if(child==null)break;}; } return i-1; } wac 11-12-2004, 05:45 PM (sigh :( ), it looks like inserting an LI doesn't cause renumbering using either <LI value=''<%=n%>' > or <OL start='0' > when an item is inserted or deleted, so I'll have to use Garadons method. Oh well, the pitfalls of DHTML. codegoboom 11-12-2004, 06:05 PM The list should be renumbered. What method are you using? wac 11-12-2004, 06:15 PM sorry I can't provide the entire code in context but this is the basics. After the insertBefore, when I check .value of each element, I get 0 for everything if I use <ol start='0'>, or I get the original values, if I put a value tag on each LI. I get no reordering. Is this another one of those stupid IE tricks? I'm using (and stuck with) IE 6+ var row = document.createElement('LI') ; row.id = id ; row.className = 'flow' ; row.style.width = width ; row.innerHTML = newtable ; var idx = 0 ; try { var children = parent.getElementsByTagName('LI') ; idx = parseInt(event.getAttributeValue('index')) -1 ; parent.insertBefore(row, children[idx]) ; } catch (err) { idx = 0 ; parent.insertBefore(row, parent.firstChild) ; } codegoboom 11-12-2004, 07:50 PM That's what the value attribute is for; it overrides auto-ordering. The list will be reordered correctly if you omit the attributes, or reassign all of them. |
| |||
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum