SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cursor focus after adding tag to text area

    Hi all,

    I am adding buttons to a text area for formatting the text. in firefox i'm losing the focus on the text area in IE7 its ok but at the end of the text. what i would like is for the focus to work in firefox and the cursor to be placed in between the tags. like you get when posting on this forum! all my closing tags are 4 characters long so I know i need to find the end of the text then minus 4 but not sure how to do this? or is there a better way of doing it?

    Code so far:
    Code JavaScript:
    function inserttag(myField, myValue) {
    //IE support
    if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
    + myValue
    + myField.value.substring(endPos, myField.value.length);
    } else {
    myField.value += myValue;
    }
    }

    Form:

    HTML Code:
    <form id="form1" name="form1" method="post" action="">
      
      <input type="button" name="bold" id="bold" value="Bold" onClick="inserttag(document.form1.arttext,'[B][EB]');" /> <input type="button" name="bold" id="bold" value="Italic" onClick="inserttag(document.form1.arttext,'[I][EI]');" /><br />
      Article:
      <textarea name="arttext" id="arttext" cols="45" rows="5"></textarea>
    
    </form>

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2007
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    createTextRange is IE only


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
  •