Forum Moderators: open
element.firstChild.data
This will work on all of the links in a page. I don't know if your script will run before the link is followed. If not, it needs to be rewritten to return false then do location.href=element.href
onload=function(){
var els=document.getElementsByTagName('a'),
i=els.length;
while(i-->0)
els[i].onclick=function(){
alert(this.firstChild.data)
}
}
You're using the link for the hover effect, not to load a new page. When the link is clicked the link text gets written into an <hx> tag?
Could you explain in more detail what you want it to do? Is there more than 1 link? Do they write to the same <hx> tag?
A menu is generated using a script like:
<SCRIPT language="JavaScript" type="text/javascript">
var currentLink;
var target = "_self";
var url = "thisPage.html";
var linkparams = 'onClick="currentLink=this.innerHTML"';
includeInterface('','links');
</SCRIPT>
Once a menu link is clicked an iframe is generated:
<SCRIPT type="text/javascript" language="JavaScript">
includeInterface(window.location.search.substring(1));
</SCRIPT>
The head contains a external script:
var interfaceOptions = '&iframew=550&iframeh=1040'
function includeInterface(options,file){
if(!file) file = 'my_interface';
options = interfaceOptions + (options? '&'+options : '');
document.write('<SCRIPT type="text/javascript" language="JavaScript" src="http://www.theirsite.com/codes/'+file+'.php?'+options+'"><'+'/SCRIPT>');}
This basically writes a menu and when a link is clicked it fills the iframe with the appropriate info. Always the same page, just the iframe content changes. My intent is to write the link text in the form of a headline above the iframe so people know what they are viewing. I don't have access to "their" code that generates the iframe else it would be easy.
To sum up, I want to capture that clicked link text and write it above the iframe....
Hope this make sense.
<script type="text/javascript">onload=function(){
var els=document.getElementsByTagName('a'),
i=els.length;
while(i-->0)
els[i].onclick=function(){
document.getElementById('headline').firstChild.data=this.firstChild.data
}
}</script>
</head>
<body>
<h3 id="headline">Default Headline</h3>
Depending on how the HTML is written / interpreted, the element may contain more than one text-node. The contents would need to be normalised.
<h3>
Hello
</h3>
This might be considered by some clients to contain an empty text node before "Hello".
Jon_King, try this page.
<html>
<head>
<title>TEST</title>
<script type="text/javascript">
onload=function(){
var els=document.getElementsByTagName('a'),
i=els.length;
while(i-->0)
els[i].onclick=function(){
document.getElementById('headline').innerHTML=this.innerHTML
}
}
</script>
</head>
<body>
<h3 id="headline">Default Headline</h3>
<a href="#">text 1</a><br>
<a href="#">text 2</a><br>
<a href="#">text 3</a><br>
<a href="#">text 4</a>
</body>
</html>