SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Form textarea

  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    <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:
    <textarea id="new_topic_text" class="new-topic-subject-text elastic" style="overflow: hidden;"></textarea>

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your javascript, refer to the <textarea> directly by inputting the <textarea>'s id into getElementById() and then it won't matter where the <textarea> is in your html.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It is invalid to have a textarea that isn't inside a form.

    For a textarea to be valid in HTML4/5 it needs to be wrapped inside a block element such as <fieldset> or <div> which in turn is inside a <form>

    HTML3.2 (which was superceded in 1997) allowed the textarea to be directly inside the form tag but still required that the form tag be there. There has never been a version of HTML that allows form fields outside of forms.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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