if i have the following links :
<a href="#" onclick="return navigateTo(this)" id="menuList:0:menu" class="normalLink">
<span id="menulist:0:menuLabel">
<span > Main Page </span>
</span>
</a>
<a href="#" onclick="return navigateTo(this)" id="menuList:1:menu" class="normalLink">
<span id="menulist:1:menuLabel">
<span > Search Orders </span>
</span>
</a>
<a href="#" onclick="return navigateTo(this)" id="menuList:2:menu" class="selectedLink">
<span id="menulist:2:menuLabel">
<span > Orders History </span>
</span>
</a>
I want to get the element which has the class name selectedLink
and then get the value of the inner span
of that element where it says in above example Order History
.
I can do the first part of finding the element which has selectedLink
class, but i cannot retrive the text inside the inner span of that element. Below is my code to find the element with class selectedLink
:
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++)
{
var classes = elem[i].className;
if(classes == 'selectedLink' ){
alert("ok found it the selected link");
}
}
Can anyone suggest a way to retrieve Order History
? Thanks
UPDATE: Follow up question
So the point of this excerise is that, i wanted to add a check where if the page is "Order History" yet the navigation menu had selectedLink
style applied to another page (happens when user presses BACK) i wanted JavaScript logic to fix the problem so below is my code:
//get the element that has "selectedLink" style
// if the text says "Order History" then style is applied ok
var elems = document.getElementsByClassName("selectedLink");
if(typeof elems != "undefined"){
for(var i = 0; i < elems.length; i++){
alert(elems.length);
var text = elems[i].innerText || elems[i].textContent;
alert(text);
if(text == "Order History"){
alert("its ok");
}
else { //we're here because the element that has "selectedLink" style in not "order History"
//get all elements that have "normalLink" style and see which one is "Order History"
var allLinks = document.getElementsByClassName("normalLink");
for(var j = 0; j < allLinks.length; j++ ){
var curText = allLinks[j].innerText || allLinks[j].textContent;
if(curText == "Order History"){
alert("there it is at counter " + j);
alert(allLinks[j].innerText);
//swap the class names
allLinks[j].className = "selectedLink";
elems[i].className = "normalLink";
break;
}
}
}
}
}
The changing class names bit isn't working! Am i doing it wrong?
UPDATE 2:
Fixed, It was my silly mistake :
changed the ordering of the following :
allLinks[j].className = "selectedLink";
elems[i].className = "normalLink";
to:
elems[i].className = "normalLink";
allLinks[j].className = "selectedLink";