Krzysztof Kujawski wrote:
a) add a string in the current cursor position?
See the article by Martin Honnen
<URL:http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130>
b) add string before and after selection in textarea e.g. selection
c) if I put with value+='' how to move cursor to position after ] and
before [
<form>
<textarea name="ta"></textarea>
<input type="button"
onclick="surroundText(this.form.elements['ta'])"
value="Surround text">
</form>
<script type="text/javascript">
function surroundText(ta){
var rng, r2;
if(document.selection &&
document.selection.type=="Text"){
rng=document.selection.createRange();
r2=rng.duplicate();
if(rng.parentElement()==ta){
rng.text="
"+rng.text+"";
r2.select(); //to select before
//rng.select(); //to select after
}
}
}
</script>
Ranges are a very useful tool, unfortunately still remaining poorly
implemented across user agents. The example given uses IE ranges and
works only on IE; there is, currently, no way to achieve the same effect
in other browsers (although Mozilla does implement W3C ranges quite well).
<URL:http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_textrange.asp>
<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html>
HTH
Yep.