Forum Moderators: open

Message Too Old, No Replies

Use GetElementByID to make script only affect some links

         

illtron

4:25 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Hi guys, I'm working on some stuff that I got help with here a while back. I'll summarize briefly:

The original thing I built relied heavily on frames. Frames suck, so I'm trying to avoid them now, by using an iframe and adding some more to the page I use (a utility for work).

I have a script that takes a link and puts part of the URL and the text of the link in a textbox. This works. The problem now, is that it affects all links on the page, and apparently interferes with another script.

Here's the script:


function addClick(){
for (i=0;i<document.links.length;i++){
document.links[i].onclick = intercept;
}
}
function intercept(){
document.getElementById('boxy').value = this.href.replace(/^.*?:\/\/.*?(\/.*)$/, "$1") + '\r' + this.innerHTML.replace(/<\/?.*?>/g, "");
}

Now, is there a way to have this script only affect links within a certain div? I tried changing document.links. to document.getElementById('thedivID').links, but with no luck.

Any suggestions on where I should go from here?

Fotiman

4:46 pm on Jan 4, 2006 (gmt 0)

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



The links[] array applies to the entire document, so you won't be able to use that. However, once you have your div element, you could get all of the decendant <a> elements.


var div = document.getElementById('thedivID');
if(!div ) return;
var aNodeList = div.getElementsByTagName("A");
for( var i = 0; i < aNodeList.length; i++ )
{
aNodeList.item(i).onclick = intercept;
}

I haven't tried that, but I think it'll work.

illtron

4:57 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Worked like a charm, thanks!