/    Sign up×
Community /Pin to ProfileBookmark

Problem with text replace script

I can’t get the script to work that will replace old text with new text in specified th:

html content:

[CODE]
<table align=”center” cellspacing=”1″ class=”homepagemodule report” id=”trade_bait”>
<caption>
<span>Trade Bait</span>
</caption>
<tbody>
<tr>
<th class=”franchisename”>Franchise</th>
<th class=”willgiveup”>Will Give Up</th>
<th class=”inexchangefor”>In Exchange For</th>
<th>Trade?</th>
</tr>
[/CODE]

I would like to replace “Will Give Up” in second th with “Offered” and here’s script I did that didn’t worked:

[CODE]
function replaceText () {
var offeredparent = document.getElementById(‘trade_bait’).getElementsByTagName(“tbody”)[0].getElementsByTagName(‘tr’).getElementsByTagName(‘th’)[1];
var str = offeredparent.textNode.nodeValue;
tr = str.replace(‘Will Give Up’,’Offered’);
offeredparent.textNode.nodeValue = str;
}
[/CODE]

Could use help to correct this or suggest different script that would work better.

appreciate the help!

johnny

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@artemisJul 08.2007 — you need to replace this:

[code=php]tr = str.replace('Will Give Up','Offered');[/code]

with:

[code=php]tr = str.replace(/Will Give Up/g,'Offered');[/code]

The 'g' means its global and doesnt just replace the first instance
Copy linkTweet thisAlerts:
@UltimaterJul 09.2007 — getElementsByTagName('tr') needs a [0]... I'd personally write this as:
<i>
</i>document.getElementById('trade_bait').rows[0].cells[1].firstChild.data="Offered"
×

Success!

Help @johnny41 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 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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