/    Sign up×
Community /Pin to ProfileBookmark

replace("eski","<b>eski</b>")

[code=php]
<script type=”text/javascript”>
function change(){
var el = document.getElementById(‘tdid’);
for(var i = 0; i< 3; i++) {
el.innerHTML = el.innerHTML.replace(“eski”,”<b>eski</b>”);
}
}
</script>
</head>
<body><table width=”100%”><tr><td id=”tdid”>
eski eski eski eski eski eski
<br><br><br><a href=”#” onClick=”change()”>change text</a></td></tr></table>
[/code]

output
[b]eski[/b] eski eski eski eski eski
I want this:
[b]eski eski eski[/b] eski eski eski

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERApr 23.2007 — What you have coded (if it works) would change the contents of innerHTML 3X.

Do a search of 'Regular Expressions in javascript" or 'RegEx' matching and replacement. I think you can then specify how many (max and min) changes will occur in a particular string.
Copy linkTweet thisAlerts:
@jaffamolApr 23.2007 — step through the loop, it is then obvious why the output is like you mentioned.

before loop:

eski eski eski eski eski eski

1st loop:

<b>eski</b> eski eski eski eski eski

2nd loop:

<b><b>eski</b></b> eski eski eski eski eski

3rd loop:

<b><b><b>eski</b></b></b> eski eski eski eski eski

It finds the first instance of eski and replaces it, I will work out a solution after my tea.
Copy linkTweet thisAlerts:
@samanyoluauthorApr 23.2007 — [code=php]
<script type="text/javascript">
var arr = [];
function change(){
var el = document.getElementById('tdid');
var L = el.firstChild.nodeValue.split(' ');
var fc = el.firstChild.nodeValue;
alert(fc);
alert(L.length); 6
for(var i = 0; i< L.length-3; i++) {
alert(L[i])
arr.push("<b>"+L[i]+"<b>"); }
alert(arr)
for(var n = 0; n < L.length -3; n++){
fc = fc.replace(fc.split(' ')[n], arr[n])
}
alert(fc);
}
</script>
</head>
<body><table width="100%"><tr><td id="tdid">eski eski eski eski eski eski<a href="#" onClick="change()">change text</a></td></tr></table>
[/code]

It did not.

JMRKER

I will think "Regular Expression"
Copy linkTweet thisAlerts:
@Mr_JApr 23.2007 — Here's one possibility

[CODE]<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<script type="text/javascript">
function change(){

el=document.getElementById('tdid')

ar = el.innerHTML.split(" ")

el.innerHTML = ""

for(var i=0;i<ar.length;i++){

if(i<3){
el.innerHTML +="<b>"+ar[i]+"</b> "
}
else{
el.innerHTML += ar[i]+" "
}

}

}

</script>
</HEAD>
<BODY>
<table width="100%">
<tr>
<td>
<div id="tdid">eski eski eski eski eski eski</div>
<br><br><br>
<a href="#" onClick="change()">change text</a>
</td>
</tr>
</table>

</BODY>
</HTML>[/CODE]
Copy linkTweet thisAlerts:
@samanyoluauthorApr 23.2007 — Thanks a lot for your interest.


This code works.
Copy linkTweet thisAlerts:
@JMRKERApr 23.2007 — Just a question as it appears to be very specialize solution for a very special problem:

Would that solution work if the .innerHTML was this?

<div id="tdid">Something else eski eski eski eski eski eski</div>
Copy linkTweet thisAlerts:
@jaffamolApr 23.2007 — It should do, I looked at the code (didnt test it) and the output should be this:

[B]Something[/B] [B]else[/B] [B]eski[/B] eski eski eski eski eski
Copy linkTweet thisAlerts:
@JMRKERApr 23.2007 — Yes, I agree, but is that what the original post wants?
Copy linkTweet thisAlerts:
@samanyoluauthorApr 27.2007 — The code I wanted:
<br/>
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Document Title&lt;/TITLE&gt;

&lt;script type="text/javascript"&gt;
function change(){

var el=document.getElementById('divid')

var ar = el.innerHTML.split(" ")
var ar2 = [];

el.innerHTML = ""
var s = 0;
for(var i=0;i&lt;ar.length;i++){

if(i&lt;ar.length &amp;&amp; ar[i] =="eski" &amp;&amp; s&lt;3){ ar2.push(ar[i]);
el.innerHTML +="&lt;b&gt;"+ar2[s]+"&lt;/b&gt; "
s++;
}
else{
el.innerHTML += ar[i]+" "
}
}
}
&lt;/script&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;table width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div id="divid"&gt;Burada eski yaz&amp;#305; ile yaz&amp;#305;lm&amp;#305;&amp;#351; eski kitaplar, eski evin bir kö&amp;#351;esinde duruyordu. El yap&amp;#305;m&amp;#305; eski bir kilim seriliydi. Su doldurdu, eski bak&amp;#305;r testiye. Sanki eski bir zamanda ya&amp;#351;&amp;#305;yordu.&lt;/div&gt;
&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="#" onClick="change()"&gt;change text&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
Copy linkTweet thisAlerts:
@s_b37Apr 27.2007 — with regexp:
[CODE]
var pattern = /(eski)/;
var str = document.getElementById('divid').innerHTML;
str.replace(pattern, "<strong>eski</strong>");
document.getElementById('divid').innerHTML = str;
[/CODE]


should work and is much neater and easier

ps: everyone look up regexp on http://www.javascriptkit.com/jsref/regexp.shtml if you are interested
Copy linkTweet thisAlerts:
@samanyoluauthorApr 27.2007 — Thanks...

If I write

el.innerHTML = el.innerHTML.replace([color=red]/eski(?=s)/[/color],"<b>eski</b>");

instead of

el.innerHTML = el.innerHTML.replace([color=red]"eski"[/color],"<b>eski</b>");

It works.
[code=php]
<script type="text/javascript">

function change(){
var el = document.getElementById('tdid');
for(var i = 0; i< 3; i++) {
el.innerHTML = el.innerHTML.replace(/eski(?=s)/,"<b>eski</b>");
}
}
</script>
</head>

<body><table width="100%"><tr><td id="tdid">
Buradaki yaz&#305;daki eski kelimeleri kal&#305;n eski olarak de&#287;i&#351;ecek. En az üç tane eski yazmam gerekiyor. Fazladan olarak eski eski eski eski.
<br><br><br><a href="#" onClick="change()">change text</a></td></tr></table>
[/code]
Copy linkTweet thisAlerts:
@s_b37Apr 28.2007 — good, but i think you need to clause (..) or else it will match:

"e s apple k doody i " i think, i havent tested

also change my reg to /(eski)/gi to ignore case and find all occurences

i forgot about that

so reg should be: /(eski)(?=s)/gi if i understand what you want properly
×

Success!

Help @samanyolu 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.3,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...