0

I have a webapp that must allow users to interactively manipulate strings (words, phrases and so on...)

Example:

given a foobar string, if the user clicks on b the string is split in two and a whitespace is added, resulting in foo bar.

I could put each single character inside a span element, but I fear this would be troublesome for long strings.

Any advice?

5
  • 1
    If you want to capture events on individual characters, you'll need to have each character in an individual element. Commented May 30, 2011 at 12:46
  • Textarea: foobar => CLICK between o and b => foo bar? Is that enough? Commented May 30, 2011 at 12:47
  • You may be interested in looking at Lettering.js, which can put letters (or words) in <span> tags for you. Commented May 30, 2011 at 12:51
  • @polarblau this may be a good start. Commented May 30, 2011 at 12:51
  • Have a look at Rangy for cross-browser selections & ranges. code.google.com/p/rangy Commented Oct 10, 2011 at 15:48

2 Answers 2

1

This version using jQuery (not necessary) should pretty much do what you need if I understood you correctly:

// Given a textarea with the content
var text = $('textarea').text().split('');

$('textarea').click(function(){
    text.splice(this.selectionStart, 0, " ");
    this.value = text.join('');
});

It's a very simple and not cross browser enabled example, but it should get you started.

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

1 Comment

Thanks, this is a good starting point. Also, thanks for the example :)
0

Yes, it will be ok, but setup your event handler not on individual spans, but on the whole container and then see here: http://en.wikipedia.org/wiki/Flyweight_pattern

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.