What I am trying to do and have mostly accomplished is changing the tag parameter in the URL for anchor tags wrapped in a span. That is working fine. what is happening is it pulls the first anchor tag href and and is replacing all the anchor tags with the same href. I thought as it went through the loop it would replace the var each time with the new href. Suggestions?
<div class="changeparam">
<span><a href="http://demo.org/post.php?i=17uh23&p=456&s=789&tag=asdf">Link</a></span>
<span><a href="http://demo.org/post.php?i=18uh23&p=456&s=789&tag=asdf">Link</a></span>
<span><a href="http://demo.org/post.php?i=19uh23&p=456&s=789&tag=asdf">Link</a></span>
<span><a href="http://demo.org/post.php?i=20uh23&p=456&s=789&tag=asdf">Link</a> </span>
<a href="www.test.com">test</a>
</div>
Here is my code that does replace the tag parameter in a anchor tag. That functionality is working fine. My issue is in the function displayTextNumber(). It is storing the first anchor tag it finds and using that to replace all the other anchor tags href across the entire site. I want to iterate through each tag.
function replaceQueryString( queryString, keys, newValues ) {
var parts = queryString.split('&');
// We're going to make an array of querystring key=value strings
var new_parts = [];
for( i in parts ) {
var keyValue = parts[i].split('=');
// Use jQuery to see if this key is in our desired set
var replacePos = $.inArray(keyValue[0],keys);
// If it is, it will give a non-negative integer, if not it'll give -1
if( replacePos >= 0 )
// We want to replace this key so make a new string for the key/value pair
new_parts.push( keyValue[0] + '=' + newValues[replacePos] );
else {
// This isn't the key we want to replace, so leave it alone
new_parts.push( parts[i] );
}
}
// glue all the parts together and return them
return new_parts.join('&');
}
function displayTextNumber(){
if(isNotBrandedTerm()){
var NumberSpans = document.getElementsByTagName('span');
for (var i=0; i < NumberSpans.length; i++) {
// Get the full address from the original link
var old_fulladdr = $('span a').attr('href');
var old_addr_parts = old_fulladdr.split('?');
// The keys you want to replace
var tobereplaced = ['tag'];
// The respective values you want to assign
var replacements = [getPhoneNumber()];
var new_query_string = replaceQueryString( old_addr_parts[1], tobereplaced, replacements );
//var new_querystring = 'i=abc&p=def&g=ghi';
$('span a').attr('href',old_addr_parts[0] + '?' + new_query_string);
}//CLOSE IF
} //CLOSE FOR
}//close isNotBrandedTerm