- The goal is for a function to append or truncate specific anchor href "text values"
Array of Tag objects and Class name is passed along with the string to append or truncate to the href "text value"
<table> <tr> <td class="columnOne"> <span class="processTitle"> <a href="sample_US.html">Sample (US version)</a> </span> <span class="updateIndicator">12/28/2014</span> </td> <td> <div class="loc_us"> <span class="templateName">Request Form</span> <span class="templateLinks"> <a target="_blank" href="Templates_Form.xls" class="linkTemplate"></a> </span> <span class="processTitle"> <a href="sample2_US.html">Sample2 (US version)</a> </span> </div> </td> </tr> </table>
Example where an initialize function calls to an append/truncate the URL:
function initProjectType() {
theSpanTags_ar = document.getElementsByTagName("SPAN")
appendURLs(theSpanTags_ar,"processTitle","?loc=US");
}
function appendURLs(whichTag_ar, whatClassName, theAppendString, URLremoveString) {
for(i=0; i<whichTag_ar.length; i++) { // loop thru the tags
if (whichTag_ar[i].className === whatClassName){ // match tag to class
var theLinkTags_ar = whichTag_ar[i].getElementsByTagName("A") // extract the Anchor into an array
for(j=0; j<theLinkTags_ar.length; j++) { // loop thru the number of Anchors
theLink = theLinkTags_ar[j].nodeValue.href; // extract the href "text value" <<<---help?
if (URLremoveString) { // if truncate the href value
if (theLink.indexOf(URLremoveString) != -1) {
theLink = theLink.substr(URLremoveString.length)
theLinkTags_ar[j].href = theAppendString + theLink;
}
} else { // else append the href value
theLinkTags_ar[j].href = theLink + theAppendString;
}
}
}
}
}
I've tried all kinds of variations to extract the text string value without success, yet given that the objects are being passed in an array, simpler methods like GetElementByID.GetByTagName do not appear to work. Also trying to stick with Javascript only.
I am probably missing a fundamental point here, and thank you in advance with helping my ongoing learning!