Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts

    determine position of an ordered list item

    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???
    Wayne Christian

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Position relative to other LI items? Position relative to the browser window?

    Sorry but you're not coming in clear yet.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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
    Wayne Christian

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Code:
    <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
    Code:
    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;
    }
    Last edited by Garadon; 11-12-2004 at 05:32 PM.

  • #5
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts
    (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.
    Wayne Christian

  • #6
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The list should be renumbered. What method are you using?
    *this message will self destruct in n-seconds*

  • #7
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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+

    Code:
    		
              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) ;
    		}
    Wayne Christian

  • #8
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    *this message will self destruct in n-seconds*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •