73pixieGirl
Programmer
Hello...I searched the old posts and found the following code (see below) and this works great for what I'm doing, but I need some help modifying the function. What I'd like is for each <a> to have an id, then based on which link is clicked, I'd like for that id to be selected which will change class='selected'. I added an id tag to each <a>, I put 'id' in place of 'this' (onclick="highlightLinks(id)"), I put an alert in the function to make sure it recognizes that id (and it does), then I tried to modify this line:
var linkList = document.getElementById("navigationDiv").getElementsByTagName("a"); to include the id element, but I can't get it to work.
The reason I need an id is b/c I'm only working with one body tag in a template, with XML pages being pulled into that template. Because different pages are being pulled into the template, each time the page is called the focus of which link was clicked gets lost. So I'm thinking if I send an id to the function, then say if(obj == id) then set that class to 'selected' it will always know which link was clicked.
So here's the original code I found from an old post:
<style type="text/css">
#navigationDiv a {
text-decoration:none;
display:block;
color:black;
margin-bottom:5px;
}
#navigationDiv a.selected {
text-decoration:underline;
}
</style>
<script type="text/javascript">
function highlightLinks(obj) {
var linkList = document.getElementById("navigationDiv").getElementsByTagName("a");
for (i = 0; i < linkList.length; i++) {
linkList.className = "";
}
obj.className = "selected";
}
</script>
<div id="navigationDiv">
<a href="#" onclick="highlightLinks(this)" class="">anchor1</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor2</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor3</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor4</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor5</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor6</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor7</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor8</a>
</div>
Did I explain that well enough?
Thank you!
var linkList = document.getElementById("navigationDiv").getElementsByTagName("a"); to include the id element, but I can't get it to work.
The reason I need an id is b/c I'm only working with one body tag in a template, with XML pages being pulled into that template. Because different pages are being pulled into the template, each time the page is called the focus of which link was clicked gets lost. So I'm thinking if I send an id to the function, then say if(obj == id) then set that class to 'selected' it will always know which link was clicked.
So here's the original code I found from an old post:
<style type="text/css">
#navigationDiv a {
text-decoration:none;
display:block;
color:black;
margin-bottom:5px;
}
#navigationDiv a.selected {
text-decoration:underline;
}
</style>
<script type="text/javascript">
function highlightLinks(obj) {
var linkList = document.getElementById("navigationDiv").getElementsByTagName("a");
for (i = 0; i < linkList.length; i++) {
linkList.className = "";
}
obj.className = "selected";
}
</script>
<div id="navigationDiv">
<a href="#" onclick="highlightLinks(this)" class="">anchor1</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor2</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor3</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor4</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor5</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor6</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor7</a>
<a href="#" onclick="highlightLinks(this)" class="">anchor8</a>
</div>
Did I explain that well enough?
Thank you!