dennis.sprengers@gmail.com wrote:
Hi Dennis,
Quote:
What I'm trying now, is to enlighten the "B" button in the toolbar,
whenever the cursor is captured between [b] tags. If the cursor is
captured between [b] and [u] tags, both these buttons should lighten
up.
To get information about cursor/caret position in a textarea you need to
manipulate "ranges", i.e. objects which represent text ranges. Not
surprisingly, there are two scriptable models available, the IE one and
the W3C one (sometimes augmented with custom methods).
IE's model has been supported by Internet Explorer since version 4 I
think, however W3C's ranges may not be supported everywhere - check
accordingly.
<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html>
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_textrange.asp>
<snip>
Quote:
Who could help me write a funcion that detects
which UBB tags the cursor in a textarea is captured between?
See below for some quick example; tested in IE7, Firefox 2 and Opera 9.
IE has problems though, as it does not seem to properly fire left/right
arrow key events in the textarea (or I may be missing something, I
haven't time to investigate now).
---
<style type="text/css".depressed { border:1px inset; } </style>
<form action="#">
<input type="button" value="B" name="B">
<input type="button" value="U" name="U">
<input type="button" value="I" name="I">
<textarea onkeyup="foo(this)" onclick="foo(this)"></textarea>
</form>
<script type="text/javascript">
function foo(textarea){
for(var tags=["B", "U", "I"], ii=0, a; ii<tags.length; ii++) {
a=textarea.
value.
substr(0, getCaretPos(textarea)).
split("[\/"+tags[ii]+"]");
textarea.form.elements[tags[ii]].className=(
a[a.length-1].indexOf("["+tags[ii]+"]")!=-1
) ? "depressed" : "";
}
function getCaretPos(el) {
var rng, ii=-1;
if(typeof el.selectionStart=="number") {
ii=el.selectionStart;
} else if (document.selection && el.createTextRange){
rng=document.selection.createRange();
rng.collapse(true);
rng.moveStart("character", -el.value.length);
ii=rng.text.length;
}
return ii;
}
}
</script>
---
Regards,
Elegie.