It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Failing to replace a string multiple times.

Failing to replace a string multiple times.

Postby Atomix on Thu Aug 19, 2010 6:07 pm

Hello!
I ran into a problem that I could not fix myself, so I am asking for your help.
I am trying to replace a string with another string, the replace lines look like this:

Code: Select all
var word = document.getElementById("word").innerHTML;

document.getElementById("word").innerHTML = wordd.replace(/B/g, '<span class="style106">B</span><span class="style107"> </span>');


It works just perfectly if left alone, but I need to replace every letter inside this string, adding those style and span tags around each and every letter. So if I add another line to this code, like this:

Code: Select all
var word = document.getElementById("word").innerHTML;

document.getElementById("word").innerHTML = wordd.replace(/B/g, '<span class="style106">B</span><span class="style107"> </span>');


document.getElementById("word").innerHTML = wordd.replace(/C/g, '<span class="style106">C</span><span class="style107"> </span>');



Then it would replace only one of those letters with the given line. For example, it would replace only letter C, but it wouldn't touch the letter B, even though those lines are EXACTLY the same.
I thought it had some problems deciding which line to execute, since they don't have any intervals between them, so I added setTimeout and ran each line with a 5 second delay, but still the result is the same - it replaces only one of the letters (executes only 1 line of code). It doesn't matter how many lines there are, I added the whole alphabet, but it still replaced only one letter.

If there is an alternative solution on how to add tags around a letter inside a string, I would like to hear it.
Please help me solve this problem, this is the only line of code that's keeping me from finishing the whole application.
Thank you!
Atomix
 
Posts: 2
Joined: Thu Aug 19, 2010 6:01 pm

Re: Failing to replace a string multiple times.

Postby Atomix on Fri Aug 20, 2010 7:39 am

Alright, nevermind the previous post, I fixed it, but I need to find why this code gives me undefined error:

You can see the code in action here:
open this link
http://www.w3schools.com/cn/html/tryit.asp?filename=tryhtml_basic

and paste this code on the left side of the webpage, then press 'edit and click me'

heres the full code:

Code: Select all
<html><head><title>eink</title></head>
<body onload="aaa()">

<script type="text/javascript">

function aaa() {
var word2 = document.getElementById('word'),
        letters = word2.innerHTML.split(''),
        spanified;
        for (var k = 0, j = letters.length; k < j; k++) {
            spanified += '<span class="style101">' + letters[k] + '</span>';
        }
        word2.innerHTML = spanified;

}


</script>

<style type="text/css">
.style101
            {
                background-color: #FF0000;
            }
</style>
<h1 id="word">HOUSE</h1>

</body>
</html>






Here is a picture, of how it looks:

Image
Atomix
 
Posts: 2
Joined: Thu Aug 19, 2010 6:01 pm


Who is online

Users browsing this forum: No registered users and 14 guests