Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Form textarea

  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Form textarea

    I found a script that is perfect for my need but it is written for forms textareas and I can seem to get it to work textareas that are not wrapped in form.
    Code:
    Code:
    <script>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(); 
    } </script>
    <button onclick="insertAtCursor('[blah]','[/blah]')">Test</button>
    <form method="post" name="form" action="">
    <textarea name="message" cols="50" rows="10"></textarea>
    </form>
    My textarea:
    Code:
    <textarea id="new_topic_text" class="new-topic-subject-text elastic" style="overflow: hidden;"></textarea>

  • #2
    New Coder
    Join Date
    Aug 2002
    Location
    Moscow, RU
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hello, just add a name to your textarea element (name="message"). because that script is looking for a textarea field named message and cannot find it - so it not works.

    Code:
    <textarea name="message" id="new_topic_text" class="new-topic-subject-text elastic" style="overflow: hidden;"></textarea>
    Last edited by Inci; 09-17-2011 at 12:01 AM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,630 Times in 4,593 Posts
    Properly, you should always put form elements such as <textarea> into a <form>...</form>.

    If you don't have a <form>, just wrap it with a dummy one:
    Code:
    <form>
    <textarea ...></textarea>
    </form>
    Won't do any harm and avoids problems in older browsers.

    But if you do *NOT* have it in a <form>, then it is better to leave it as is. Don't give it a name.

    Instead, just change the JS code from
    Code:
      var obj = document.form.message;
    to
    Code:
       var obj = document.getElementById("new_topic_text");
    For that matter, you can use that code whether or not you put it in a <form>.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •