/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] JavaScript replace html code

Hi.

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.

Basically, I have a code like this:

[code=html]<table border=”1″ cellpadding=”0″ class=”my_table”>
<tr>
<td>Text</td>
</tr>
</table>[/code]

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>[/CODE]

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?)

Can anybody help me out?

Thanks.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jan 21.2010 — document.getElementById("give_the_table_an_id").border = "1";

innerHTML and tables don't go well together in IE as far as I remember.
Copy linkTweet thisAlerts:
@meekauthorJan 21.2010 — Hi.

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?

Regards.
Copy linkTweet thisAlerts:
@hoangkcJan 21.2010 — Try this function
[CODE]/*
* $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;
}[/CODE]
Copy linkTweet thisAlerts:
@meekauthorJan 21.2010 — Hi and thank you for your help. Much appreciated.

I'm not an expert, so could you also tell me how to use it, please?

Thanks.
Copy linkTweet thisAlerts:
@Declan1991Jan 21.2010 — I'd use [url=http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/]Ultimate getElementsByClassName[/url] despite the name, if you intend on doing this much. If the first table with that class name, use<i>
</i>getElementsByClassName("whateverIsTheClass","table")[COLOR="Red"][B][0][/B][/COLOR].border = 1;
If the first element on the page with that class name, usegetElementsByClassName("whateverIsTheClass")[COLOR="Red"][B][0][/B][/COLOR].border = 1;The number in square brackets can be changed to refer to the second etc.
Copy linkTweet thisAlerts:
@meekauthorJan 22.2010 — @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?

[CODE]getElementsByClassName("login-headline","table")[0].border = 0;[/CODE] So basically, I don't know how to run the script... Can you help me out?
Copy linkTweet thisAlerts:
@Declan1991Jan 22.2010 — Copy and paste his function into where ever you have your JavaScript. Maybe have it as the first thing in the file. Then your function. Something like&lt;script type="text/javascript"&gt;
/* His code
...
...
*/
function replace_stuff () {
// Your code
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@meekauthorJan 25.2010 — Thanks a million! It works perfectly. You have been a great help.

Thanks again.
×

Success!

Help @meek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 1.26,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @dert,
tipped: article
amount: 1000 SATS,

tipper: @viney352,
tipped: article
amount: 10 SATS,
)...