Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retreiving an item() number without looping? 1

Status
Not open for further replies.

corchard

Programmer
Jul 3, 2002
23
CA
I need to retreive a page item number without looping so I can reference it. I currently have code set up like this, but it can take upwards of 45 seconds to process if there are a lot of items on the page:

function CalClick(theObject){
for(var i=0;i<99999;i++)
if(theObject==document.all.item(i))
return i;
return -1;
}

<img src=&quot;some.gif&quot; onClick=&quot;alert(CalClick(this));&quot;>

Is there a way I can pass the object item number straight from the item?
I would specify the item name (<img src=&quot;some.gif&quot; name=&quot;a&quot; onClick=&quot;alert(CalClick('a'));&quot;>), or hard code the number in the object (<img src=&quot;some.gif&quot; onClick=&quot;alert(CalClick(12));&quot;>), but I am relying on repeating/copying this onClick code throughout the page and need it to automatically know what item is was referenced/clicked from.

Cheers,

C.
 
You can speed up your function by aliasing document.all.item:

function CalClick(theObject)
{
var everything=document.all.item;
for(var ei=0;ei<everything.length;ei++)
{
if(theObject==everything[ei]) return ei;
}
return -1;
}
 
Thank you for the reply trollacious, I had thought of this but it hadn't worked.
in creating an alias, as you have described, on a document in excess of 8,000 items, your everything.length yielded only an 8, therefore it couldn't complete.
But even if the everything.length was correct, would it not work the same as my code? It would return once it reached the correct item, it would not completely cycle through the 9999 loops anyway.

What I really need is a way for the object to specify it's item number before it initiates the CalClick() function, so I can stay away from potentially time-consuming loops.

Cheers,

C
 
If your underlying page is static for a moment you can easily add a cross reference to each item --
Code:
  for (iI = 0; iI < document.all.length; iI++) {
    document.all.item[iI].xRef = iI
    }
which allows you to simply
Code:
  onclick=&quot;alert(this.xRef)&quot;

Real world implementations get tricky with insertions, deletions, but are quite manageable. What's the application? Game? Web page utility?
 
Thanks Wray, hadn't thought of this, great idea!
...doing this won't play havoc wioth resources on older machines/browsers will it?

The application is for a pop-up calendar for web forms.

I've tried to keep all code in an onclick string in an image which simply affects the text field directly to it's left. So I'm not sure if I'm defeating the purpose with your idea given it will have to be an additional onload function and not included with each onclick event. ...though I suppose I could have your xref function trigger on the first request of the calendar popup, ...now my mind is flowing, thanks for your help.

C
 
xRef pastes a property onto every element on the page. This shouldn't unduly burden most platforms capable of running IE 4+ (your code fragment references document.all). I've simulated elections by individually counting millions of cyber votes in just a few seconds. (Until I pushed into virtual memory then the seconds quickly turned into minutes.)

Your older machines could probably use a RAM upgrade. (Is the disk light on during the 45 second probes?) I was able to upgrade my poor old (nearly 3 years!!) Celeron from 65mb to 192mb with wonderful results.
 
Ok here is my javascript calendar, it can be used as a popup or on a div within your page.
It is not yet finished because I cannot seem to get Mozilla to return a good navigator.language string to me.

see thread216-311497
 
Ok here is my javascript calendar, it can be used as a popup or on a div within your page.
It is not yet finished because I cannot seem to get Mozilla to return a good navigator.language string to me.

see thread216-311497


my pref post did not work out verry wel.
this is the location of my calendar:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top