HighDots Forums  

Re: Modifying A tag onclick events dynamically - am I crazy

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Modifying A tag onclick events dynamically - am I crazy in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
bobzimuta
 
Posts: n/a

Default Re: Modifying A tag onclick events dynamically - am I crazy - 08-04-2006 , 06:08 PM






GaryB wrote:
<snip>
Quote:
However, if there's more than one link on the page, my code assigns
each onclick event to all of the previous nodes in the array instead of
each one independently. (I've commented the problematic line of
javascript, with "THIS IS THE PROBLEM LINE"). I must be missing
something!
snip
for(var d=0;d<docArray.length;d++)
{
if(docArray[d].className=='doc')
{
//Get values
fontNode = docArray[d];
sourceHTML = fontNode.innerHTML;
sourceHref=(sourceHTML.substring(sourceHTML.indexO f('"')+1,sourceHTML.indexOf('">')));
To get the 'href' of the anchor tag I'd suggest using
var anchor = fontNode.getElementsByTagName( 'A' )[0];
var href = anchor.href;

Quote:
path = sourceHref.substring(sourceHref.indexOf('/',8));
doc = path.substring(path.lastIndexOf('/')+1);
ext = doc.substr(doc.length - 3);
docName = fontNode.innerText;
//Determine image
switch(sourceHTML.substr(sourceHTML.indexOf('>')-4,3))
{
case 'doc' : img="msword.png"; break;
case 'xls' : img="msexcel.png"; break;
case 'ppt' : img="mspowerpoint.png"; break;
case 'pdf' : img="pdf.png"; break;
}
linkNode = fontNode.children(0);
Again,
linkNode = fontNode.getElementsByTagName( 'A' )[0];
if you had a space like this ( underscore representing space )
<font>_<a..>
children(0) == ' ' .. At least that's how it is using DOM childNodes
array

Quote:
linkNode.setAttribute('href','#');
Don't remove the href

Quote:
imageNode =
linkNode.insertBefore(document.createElement('img' ),linkNode.firstChild);
imageNode.src = 'Skins/Standard/'+img;
imageNode.alt = docName;
linkNode.onclick = function(){loadDoc(sourceHref)};//THIS IS THE
PROBLEM LINE
}
linkNode.onclick = function() { loadDoc( this.href ); return false };
the return false; statement prevents the browser from taking the
default action, which is to follow the link

Quote:
}
}
function loadDoc(filespec)
{
//Dummy code to test the onclick event coding
alert(filespec);
}
window.onload = ModifyLinks;
//--
/script
/head

body
h3>Dynamically Modifying A Tags</h3

font class='doc'><a
href="http://localhost:1411/UserData/Test1.doc">Document
1</a></font><br /
font class='doc'><a
href="http://localhost:1411/UserData/Test1.xls">Spreadsheet
1</a></font><br /
font class='doc'><a
href="http://localhost:1411/UserData/Test1.ppt">Presentation
1</a></font><br /
font class='doc'><a
href="http://localhost:1411/UserData/Test2.doc">Document
2</a></font><br /

/body
/html

I'd be very grateful for your help - can't figure it out!

Thanks very much.

Regards

Gary

gb at prodsol dot co dot nz


Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.