I have a few lines of code which I would like to be able to change. I can only do it client side, so I'm thinking a javaScript replace function might do the trick.
I would like to change border="1" into border="0" - how do I do that?
I have played with this code:
Code:
<script type="text/javascript">
function replace_stuff() {
document.body.innerHTML = document.body.innerHTML.replace(/border="1"/g,'border="0"');
}
</script>
but it doesn't work in IE. (When replacing ordinary words with this script, it works fine in Firefox and IE...is it an escaping issue or should it be done in another way?)
That seems to do what I'm looking for. It works in both Firefox and Internet Explorer. Only thing is - I only have access to a class on that table, not an id. Can the script be changed to work with a class instead of an id?
/*
* $TagClass
* Find and return elements which belong to parent element and have className, tag match search conditions
* $TagClass("className",parent,"input")
*/
$TagClass = function(className, parent, tag)
{
tag = tag||"*";
parent = parent||document.body;
var children = parent.getElementsByTagName(tag);
var elements = [], child, pattern = new RegExp("(^|\\s)" + className + "(\\s|$)");
for (var i = 0, length = children.length; i < length; i++)
{
child = children[i];
var elementClassName = child.className;
if (elementClassName.length == 0) continue;
if (elementClassName == className || elementClassName.match(pattern)) elements.push(child);
}
return elements;
}
@Declan1991 - thanks, this looks like a brilliant solution. I have inserted the script in the <head> of my page, but where in the script do I put his line?
Bookmarks