Okay I'm probably being thick, but I can't get something to work and its bugging me why. I'm using the global replace property in Javascript, but whenever I do it wont work outisde the DIV I'm in.
The DIV I'm in isnt the one I need to target, but a simplified example is below.
<div id="foo">
<a href="http://www.somesite.com" target="_blank" class="footer">Site 1</a>
</div>
<script type="text/javascript">
window.onload = function replaceScript() {
var replacement = '<a href="http://www.somesite.com" target="_blank" class="footer">Site 1</a>';
var text = '<a href="http://www.othersite.com" title="Other Site" target="_blank">Site 2</a>';
document.getElementById("foo").innerHTML = text.replace(new RegExp(replacement, 'g'), '');
}
</script>
The other way I was trying it was this:
<script type="text/javascript">
window.onload = function replaceScript() {
var toReplace = '<a href="http://www.somesite.com" target="_blank" class="footer">Site 1</a>';
var replaceWith ='<a href="http://www.othersite.com" title="Other Site" target="_blank">Site 2</a>';
document.getElementById("foo") = document.body.innerHTML.replace(toReplace, replaceWith);
}
</script>
But I can't get that one to work globally,
-
You have to escape all special characters first, there are a bunch there... Then pass the string into the regex constructor. Check this question though because you may not want to do this with regex but with the DOM instead.elclanrs– elclanrs2013年06月16日 20:55:21 +00:00Commented Jun 16, 2013 at 20:55
-
I didnt know that you couldnt do that with regex, sorry. The one below I tried and works fine except will only target in the div its inJames J– James J2013年06月16日 21:04:27 +00:00Commented Jun 16, 2013 at 21:04
3 Answers 3
I don't think you want to be doing this with a regex, you should use DOM methods instead, like this
HTML
<div id="foo">
<a href="http://www.somesite.com" target="_blank" class="footer">Site 1</a>
</div>
Javascript
var fooA = document.getElementById("foo").children[0];
fooA.href = "http://www.othersite.com";
fooA.title = "Other Site";
fooA.firstChild.nodeValue = "Site 2";
Result
<div id="foo">
<a href="http://www.othersite.com" target="_blank" class="footer" title="Other Site">Site 2</a>
</div>
On jsfiddle
8 Comments
not sure what you are trying to do ,but it makes sense to change your code.
var child= document.querySelector("#foo a");
child.href="http://otherside.com"
child.title="Site2"
child.innerText= "Site2"
works on IE>=8 and all the other browers.
1 Comment
Instead of using string comparisons, you should use DOM elements. This also creates more readable and easier to maintain code, because it is more descriptive.
var element = document.getElementById("foo");
var links = element.getElementsByTagName("a"); // get all links that are children of the div foo
for (var i = 0; i < links.length; i++) { // Loop over the list
if (links[i].getAttribute("href") == "http://www.somesite.com" ) { // Identify the link(s) we need to change
links[i].setAttribute('href', 'http://otherside.com');
links[i].setAttribute('title', 'Site2');
links[i].innerHtml = 'Site2';
// note that links[i].textContent would be better but it is not as cross browser compatible
}
}
Note that this does not require any javascript frameworks. If you are using a framework like jQuery, you might be able to simplify this.
1 Comment
getAttribute and setAttribute compatability, veerasundar.com/blog/2010/12/javascript-getattribute-method