0

I have text in a div with each character of my text enclosed in span tag:

Text = A quick brown fox jumps

<div id="span_text">
<span>A</span><span>&nbsp;</span><span>q</span><span>u</span><span>i</span><span>c</span><span>k</span><span>&nbsp;</span><span>b</span><span>r</span><span>o</span><span>n</span><span>&nbsp;</span><span>f</span><span>o</span><span>x</span><span>&nbsp;</span><span>j</span><span>u</span><span>m</span><span>p</span><span>s</span>
</div>

The problem is that the words do not wrap around since each character is enclosed in span tag. Is there any way to wrap text like in MS Word and other editors while having characters enclosed in span ???

Why do I need to enclose each character in span? Actually I am making a basic typing tutor. Whenever a key is pressed, I have to:

  1. Highlight the character in Gray if valid character was typed.
  2. Highlight the character in Red if wrong character was typed.
  3. Highlight the next character to type is Green.

So to target each character, I have to enclose them in span and give each span unique ID to select them via jQuery.

5
  • Won't they get lonely all by themselves? Commented Dec 13, 2011 at 20:05
  • What are you doing that requires each letter be in a span? There could be better workarounds. Commented Dec 13, 2011 at 20:06
  • "Mouse Food", I have updated my question and explain the reason why I am enclosing each character in span tag. Commented Dec 14, 2011 at 3:18
  • 1
    I agree with Mouse Food, there should be a better way to do this. For example, the characters that were already typed -- those don't need to go in individual spans, right? Just the current and next characters need to have different styles applied. Right? Commented Dec 14, 2011 at 4:33
  • I thought about your way but it didn't work. I'll try again and put update here. Thanks Commented Dec 14, 2011 at 5:12

5 Answers 5

2

Is the <span>&nbsp;</span> really necessary?

If you can just avoid wrapping that 1 character in a span, it should wrap just fine. I can't imagine why you would need to style a non breaking space, or access it with javascript. If you do, why?

The other characters are fine inside the spans, but I think you can move the &nbsp; out and not have it wrapped in a span.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi "alivehour", I've just updated my question explaining the reason why I am enclosing each character in span.
1
function GetWrapedText(text, maxlength) {    
    var resultText = [""];
    var len = text.length;    
    if (maxlength >= len) {
        return text;
    }
    else {
        var totalStrCount = parseInt(len / maxlength);
        if (len % maxlength != 0) {
            totalStrCount++
        }

        for (var i = 0; i < totalStrCount; i++) {
            if (i == totalStrCount - 1) {
                resultText.push(text);
            }
            else {
                var strPiece = text.substring(0, maxlength - 1);
                resultText.push(strPiece);
                resultText.push("<br>");
                text = text.substring(maxlength - 1, text.length);
            }
        }
    }
    return resultText.join("");
}

Comments

0

Get them out of the div if they don't need to be in there?

Put whatever you want to wrap around inside the div?

Comments

0

Try this:

(function () {
    var a = document.getElementById("span_text");
    a.innerHTML = a.innerHTML.replace(/<\/span><span>/g, "\n").replace(/<\/?span>/g, "");
    a.style.whiteSpace = "pre";
    a.style.width = "1.5em";
    a.style.overflow = "scroll";
}());

Comments

0

After reviewing the comments, I finally made it without enclosing each character in "span" tag. I have made two spans. One for typed characters and other for current character. On each keyup event, I update the div content by replacing the current HTML with updated HTML. Below is the basic logic I am using:

var Text = "The quick brown fox jumps over the lazy dog. It was dark and quiet in the room.";
var ChTyped = 0; // Number of characters typed so far

function update(){
  var StringTyped = Text.substr(0, ChTyped);
  var StringCurrent = '<span class="l-active">' + Text.substr(ChTyped, 1) + '</span>';
  var StringRemaining = Text.substr(ChTyped+1);
}

$("#typingbox").on('keyup', function(e){
  ChTyped++;
  update();
});

Thank you all...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.