Home N-13 News Forums Help Search
RegisterForgot password?
How to add image to post necklacesdiscou
Known bugs - 4.0.3 necklacesdiscou

Latest N-13 News 4.0.3

What is N-13 News?
Where can I get help?
Known bugs

Forums Tutorials [JS] insertAtCursor (supports wrap)
Calamity
Posted on 25 Jul 2009, 03:58:13

Access: Member
Total Posts: 33
Joined: 2008-04-11

Sorry, this isn't exactly php. But it can be used with php.
Thought it would be nice to post something.

First JavaScript I've ever written.

Code:

function insertAtCursor(sTag,eTag) 
{ 
  var obj = document.form.message; 
  var strPos;
  var ie_strPos;
  var oSel;
  obj.focus(); 

  strPos += sTag.length + eTag.length;
  ie_strPos += sTag.length + eTag.length;

  if (document.selection && document.selection.createRange) { // Internet Explorer  
    oSel = document.selection.createRange();
    if (oSel.parentElement() == obj) {
        oSel.text = sTag + oSel.text + eTag;
        ie_strPos += oSel.text.length;
        }
    oSel.moveStart ('character', ie_strPos);
    oSel.moveEnd ('character', 0);
    oSel.select ();    
  } 

  else if (typeof(obj) != "undefined") { // Firefox 
    var longueur = parseInt(obj.value.length); 
    var selStart = obj.selectionStart; 
    var selEnd = obj.selectionEnd; 

    obj.value = obj.value.substring(0,selStart) + sTag + obj.value.substring(selStart,selEnd) + eTag + obj.value.substring(selEnd,longueur); 
    strPos += txtarea.value.substring(selStart,selEnd).length;

    obj.selectionStart = strPos;
    obj.selectionEnd = strPos;
    obj.focus ();
  } 

    else obj.value += sTag + eTag; 

    obj.focus(); 
} 


to use it use this =D
Code:
<button onclick="insertAtCursor('[blah]','[/blah]')">Test</button>
<form method="post" name="form" action="">
<textarea name="message" cols="50" rows="10"></textarea>
</form>
#696
Last edited by Calamity at 2009-07-25 04:05:13 Reason:
Network-13.com © 2013