Forum Moderators: open

Message Too Old, No Replies

Javascript Onclick - display text

         

seenlihner

2:33 am on Aug 21, 2007 (gmt 0)

10+ Year Member



I'm trying to display a value from a javascript function after click click on text. I'm new to javascript and noticed the document.write() function executes once when the page is loaded. How can i get it to update after each time i click on a link. For example, I have four links: Baseball, Basketball, Football, Hockey and when i click on the one of the links, i want to display the link name in Bold letters further down on my page. There might be easier methods, but I'm filtering data with a function when the links are clicked and thought i could throw a variable with the text that i want on the end...but i don't know how to refresh the text without refreshing the whole page.

Thanks,
Nate

birdbrain

3:45 am on Aug 21, 2007 (gmt 0)



Hi there seenlihner,

try it like this...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">

</style>

<script type="text/javascript">
window.onload=function(){
anc=document.getElementById('sports').getElementsByTagName('a');
for(c=0;c<anc.length;c++) {
anc[c].onclick=function() {
showSport(this.firstChild.nodeValue);
}
}
}
function showSport(word){
document.getElementById('linkName').firstChild.nodeValue=word;
}
</script>

</head>
<body>

<ul id="sports">
<li><a href="#">Baseball</a></li>
<li><a href="#">Basketball</a></li>
<li><a href="#">Football</a></li>
<li><a href="#">Hockey</a></li>
</ul>

<div id="linkName">&nbsp;</div>

</body>
</html>

birdbrain