Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-10-2004, 07:44 PM   PM User | #1
wac
Regular Coder

 
wac's Avatar
 
Join Date: Sep 2002
Location: Cary, North Carolina, USA
Posts: 359
Thanks: 2
Thanked 0 Times in 0 Posts
wac is an unknown quantity at this point
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
wac is offline   Reply With Quote
Old 11-10-2004, 09:20 PM   PM User | #2
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
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.
Roy Sinclair is offline   Reply With Quote
Old 11-12-2004, 04:19 PM   PM User | #3
wac
Regular Coder

 
wac's Avatar
 
Join Date: Sep 2002
Location: Cary, North Carolina, USA
Posts: 359
Thanks: 2
Thanked 0 Times in 0 Posts
wac is an unknown quantity at this point
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
wac is offline   Reply With Quote
Old 11-12-2004, 05:19 PM   PM User | #4
Garadon
Regular Coder

 
Join Date: Jul 2002
Posts: 698
Thanks: 0
Thanked 0 Times in 0 Posts
Garadon is an unknown quantity at this point
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..
Garadon is offline   Reply With Quote
Old 11-12-2004, 05:45 PM   PM User | #5
wac
Regular Coder

 
wac's Avatar
 
Join Date: Sep 2002
Location: Cary, North Carolina, USA
Posts: 359
Thanks: 2
Thanked 0 Times in 0 Posts
wac is an unknown quantity at this point
(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
wac is offline   Reply With Quote
Old 11-12-2004, 06:05 PM   PM User | #6
codegoboom
Regular Coder

 
Join Date: Aug 2004
Location: codegoboom@yahoo.com
Posts: 999
Thanks: 0
Thanked 0 Times in 0 Posts
codegoboom is an unknown quantity at this point
The list should be renumbered. What method are you using?
__________________
*this message will self destruct in n-seconds*
codegoboom is offline   Reply With Quote
Old 11-12-2004, 06:15 PM   PM User | #7
wac
Regular Coder

 
wac's Avatar
 
Join Date: Sep 2002
Location: Cary, North Carolina, USA
Posts: 359
Thanks: 2
Thanked 0 Times in 0 Posts
wac is an unknown quantity at this point
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
wac is offline   Reply With Quote
Old 11-12-2004, 07:50 PM   PM User | #8
codegoboom
Regular Coder

 
Join Date: Aug 2004
Location: codegoboom@yahoo.com
Posts: 999
Thanks: 0
Thanked 0 Times in 0 Posts
codegoboom is an unknown quantity at this point
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*
codegoboom is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:49 AM.


Advertisement
Log in to turn off these ads.