SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This is a great script but how do I...

    Here's the script:

    Code:
    <script language="javascript" type="text/javascript">
    function addtext() {
    	var newtext = document.myform.inputtext.value;
    	if (document.myform.placement[1].checked) {
    		document.myform.outputtext.value = "";
    		}
    	document.myform.outputtext.value += newtext;
    }
    </script>
    
    <form name="myform">
    <table border="0" cellspacing="0" cellpadding="5"><tr>
    <td><textarea name="inputtext"></textarea></td>
    <input type="radio" name="placement" value="append" checked> Add to Existing Text<br>
    <td><p><input type="radio" name="placement" value="replace"> Replace Existing Text<br>
    <input type="button" value="Add New Text" onClick="addtext();"></p>
    </td>
    <td><textarea name="outputtext"></textarea></td>
    </tr></table>
    </form>
    This script is going to work great in conjunction with my PHP script however the problem is with the "Add to Existing Text".

    If I have three lines such as:

    Apples
    Bananas
    Oranges

    And I ""Add to Existing Text" which has "Strawberries, blueberries" it does so in this fashion:

    Code:
    Strawberries
    blueberries Apples 
    Bananas
    Oranges
    As oppsed to:

    Code:
    Strawberries
    blueberries 
    Apples 
    Bananas
    Oranges
    Does anyone know how to add that <br /> at the beginning if text is being added?

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about using a new line escape sequence, "\n"?
    Code:
    document.myform.outputtext.value += "\n" + newtext;

  3. #3
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,276
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Yes, I think this is a better idea, mostly because if you did manage to add a br, you'd get

    <textarea>Strawberries<br/>blueberries<br/>Apples<br/>Bananas<br/>Oranges</textarea>

    I don't believe you can have tags inside a textarea element, and if you can, they should be interpreted as plain text (cdata), not real br's.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, much appreciated. Since were on the topic would it be difficult to make the first instance without a "\n". The reason is that if the new textarea is blank it adds a blank space in the first entry. As I'm using the information in this form I'll have to restructure everything if the first entry is blank.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by soma56 View Post
    Thanks guys, much appreciated. Since were on the topic would it be difficult to make the first instance without a "\n". The reason is that if the new textarea is blank it adds a blank space in the first entry. As I'm using the information in this form I'll have to restructure everything if the first entry is blank.

    Code:
    document.myform.outputtext.value += 
    ((document.myform.outputtext.value)?'\n':'')+newtext;
    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="^$">

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script language="javascript" type="text/javascript">
    function addtext() {
       var newtext = document.myform.inputtext.value;
       if (document.myform.placement[1].checked) {
          document.myform.outputtext.value = "";
       }
       
       var outText = document.myform.outputtext;
       outText.value += outText.value.length > 0 ? "\n" + newtext : newText;
    }
    </script>

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Felgall...your help is greatly appreciated


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
  •