Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 47

Thread: textarea using ENTER

  1. #11
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Is this what you are looking for Auriaks?

    Code:
    <script type="text/javascript"> 
    function insertAtCursor(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 == �e0�Œ) {
        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;
      }</script>
    <script type="text/javascript"> 
    function saveCurRng() { 
    curRng = document.selection.createRange().duplicate(); 
    } 
    function surround(btag, etag){ 
    if (curRng) { 
    document.editform.article.focus(); 
    curRng.text= btag + curRng.text + etag; 
    return false; 
    } 
    } 
    </script> 
    </head> 
    <body> 
    <form name="editform"> 
    <textarea rows=5 cols=40 name="article" onKeyup="saveCurRng()" onMouseup="saveCurRng()"></textarea> 
    </form> 
    <a href="#" onclick="return surround('<b>', '</b>');">bold</a><br> 
    <a href="#" onclick="return surround('<i>', '</i>');">italic</a>
    Actually the type of line break produced by the enter key depends upon the OS:
    Do you mean it depends on what type of server PHP is running on? For example I use PHP that runs on a remote server that is Linux based. My home computer is Windows XP. The PHP code that I use to convert enter key into <BR> is
    Code:
    $text=str_replace('\r\n','<br>',$text);
    Last edited by james438; 11-16-2009 at 03:06 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #12
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by james438 View Post
    Code:
      //MOZILLA/NETSCAPE support
      else if (myField.selectionStart || myField.selectionStart == �e0�Œ) {
    This may be an OS/installed fonts thing, but the above quoted and highlighted code looks like garbage here. Also, if the value of myField.selectionStart is that highlighted thing, and that highlighted thing is truthy, it need not be tested for at all because as soon as if (myField.selectionStart evaluates as truthy, the code conditional upon the else if statement will execute. Unless, of course, if you really meant &&, not ||.

    Quote Originally Posted by james438 View Post
    Do you mean it depends on what type of server PHP is running on?
    I'm really unclear myself on that one. I would tend to think though that the sending or client computer would dictate what sort of line break is issued when the user hits ENTER. However, it is entirely possible that some sort of automatic conversion occurs on the server, in which case it would be the server that determines this.

    Somewhat related, in a textarea, if the enter key is not used and the textarea's wrap attribute (deprecated but still used and supported) is set to 'hard', when the form is submitted text wrapped by the textarea's dimension's constraints will get the server's OS's style of line break applied to the form's generated POST or Get data.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #13
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    thanks for catching that jschuer1. That wasn't the code I originally posted, so it looks like some of it was lost in translation.

    What is a truthy?

    Try this Auriaks,
    Code:
    <script type="text/javascript"> 
    function insertAtCursor(myField, myValue) {
      //IE support
      if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
      }
    else {
        myField.value += myValue;
      }</script>
    <script type="text/javascript"> 
    function saveCurRng() { 
    curRng = document.selection.createRange().duplicate(); 
    } 
    function surround(btag, etag){ 
    if (curRng) { 
    document.editform.article.focus(); 
    curRng.text= btag + curRng.text + etag; 
    return false; 
    } 
    } 
    </script> 
    </head> 
    <body> 
    <form name="editform"> 
    <textarea rows=5 cols=40 name="article" onKeyup="saveCurRng()" onMouseup="saveCurRng()"></textarea> 
    </form> 
    <a href="#" onclick="return surround('<b>', '</b>');">bold</a><br> 
    <a href="#" onclick="return surround('<i>', '</i>');">italic</a>
    It isn't perfect though because it doesn't work in Firefox. I never took the time to get it to work in Firefox. The code is not originally mine either, but some code I got from somewhere else on this forum some time ago. If anyone knows how to get it to work in Firefox I would be very grateful. Either way, I use Opera almost exclusively and have found this script to be quite handy and customizable.
    Last edited by james438; 11-16-2009 at 10:41 AM. Reason: grammar
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #14
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The term 'truthy' is used in reference to if, if else and other coding constructs that need to determine if something is true or false. It's opposite is, you guessed it, 'falsey'. These are useful concepts because they help us understand what will happen in these constructs, and to determine if they are really set up in such a way to accomplish our aims for them.

    If something has any value other than false, null, or 0, and is not undefined or an empty string, it is truthy. If you have something that tests on the basis of || (or), once one item in the series tests truthy, the rest are disregarded and the code conditional upon the test executes. So to 'ask':

    Code:
    if(something || something == 'bob')
    is superfluous because if something equals 'bob', it is truthy and the second half of the expression will be ignored because the script parser already knows the answer to be true or truthy with the first half, will skip evaluating the second part, and go straight to the code this condition executes when it is true.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #15
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    YES, thats what I was looking for No possibility for using in firefox? Because i have webpage designed for opera and firefox.

  6. #16
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    JavaScript will work in the same way regardless of the browser. Or is this not what you meant?

  7. #17
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Schmoopy View Post
    JavaScript will work in the same way regardless of the browser.
    Er, no. Different browsers have different names for the same or similar objects and methods.

    Opera is a bit odd in that it supports several of the IE centric methods and objects while at the same time supporting most of the standard methods and objects used by most others.

    IE is playing a slow game of catch up in supporting the standard objects and methods. With each new IE version, they add in support for a few more.

    Virtually all browsers have quirks - methods or objects that only it supports, and/or a slightly different 'take' on some of the standard objects or methods it does support.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #18
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Ah right, well I knew they all behaved slightly differently, but I thought it was a rare occurrence that what works for one browser won't work for another.
    Thanks for the correction.

  9. #19
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, with basic stuff, yes most modern browsers now behave the same. The major difference/exception that I can think of offhand is addEventListener vs. attachEvent. This is fairly basic stuff, and frankly IE gets it wrong clinging exclusively to its non-standard attachEvent. Opera supports both, all others afaik, just the standard addEventListener.

    Now as far as the code from james438 goes, he even stated that it didn't support Firefox. The methods for manipulating selections do still vary widely between IE and the rest, with Opera possibly supporting both methods.

    When I get a chance I will probably look this up. I have working code for it somewhere.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #20
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    So, if i will use this script it will not work in firefox? It is possible to change script somehow?

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
  •