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 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Insert Text Into One of Multiple Textareas

    Ok, I've given up on the insert text at carat idea, it seems to be impossible cross browser. I decided I will focus on getting help for this problem then.

    How do I get a link to insert text in the textbox that was last focused upon?

    PHP Code:
    <form name="form1" action="">
         <
    textarea name="textarea1" onfocus="focus(this)"></textarea>
         <
    textarea name="textarea2" onfocus="focus(this)"></textarea>
         <
    a href="javascript:insert('<img src=&quot;picture1.gif&quot; alt=&quot;picture1.gif&quot;>')">picture1.gif</a>
     </
    form
    I'm no great shakes at writing javascript. Everything I've tried for this fails, but what I think it needs would be a simple function 'focus' that knows what textarea was last focused on. Then when you click on the link 'picture1.gif', it runs the second function 'insert' that sticks '<img src="picture1.gif" alt="picture1.gif">' into the last focused upon textarea.

    That sound right? Would anyone be so kind as to help me out with the javascript portion of it?

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Do you mean something like this



    <script type="text/javascript">
    <!--
    function chk(n){
    v=n
    }

    function insert(str){
    if(v==1){
    document.forms["form1"]["textarea1"].value=str
    }

    if(v==2){
    document.forms["form1"]["textarea2"].value=str
    }

    }
    //-->
    </script>


    <form name="form1" action="">
    <textarea name="textarea1" onclick="chk(1)"></textarea>
    <textarea name="textarea2" onclick="chk(2)"></textarea>
    <a href="#null" onclick="insert('<img src=&quot;picture1.gif&quot; alt=&quot;picture1.gif&quot;>')">picture1.gif</a>
    </form>
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New Coder
    Join Date
    Apr 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mr J
    Do you mean something like this



    <script type="text/javascript">
    <!--
    function chk(n){
    v=n
    }

    function insert(str){
    if(v==1){
    document.forms["form1"]["textarea1"].value=str
    }

    if(v==2){
    document.forms["form1"]["textarea2"].value=str
    }

    }
    //-->
    </script>


    <form name="form1" action="">
    <textarea name="textarea1" onclick="chk(1)"></textarea>
    <textarea name="textarea2" onclick="chk(2)"></textarea>
    <a href="#null" onclick="insert('<img src=&quot;picture1.gif&quot; alt=&quot;picture1.gif&quot;>')">picture1.gif</a>
    </form>
    Well, that's halfway there. I want it to add the contents of the insert statement rather than just deleting everything in the textarea and adding it.

    That seems to be the only thing. When I tested it, I added several links with different statements to insert and that worked just fine.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    How about this.



    <script type="text/javascript">
    <!--
    v=0
    function chk(n){
    v=n
    }

    function insert(str){
    if(v==0){
    return
    }
    if(v==1){
    document.forms["form1"]["textarea1"].value+=str
    }

    if(v==2){
    document.forms["form1"]["textarea2"].value+=str
    }

    }
    //-->
    </script>


    <form name="form1" action="">
    <textarea name="textarea1" rows="10" onclick="chk(1)"></textarea>
    <textarea name="textarea2" rows="10" onclick="chk(2)"></textarea>
    <a href="#null" onclick="insert('<img src=&quot;picture1.gif&quot; alt=&quot;picture1.gif&quot;>')">picture1.gif</a>
    <a href="#null" onclick="insert('Hello')">Hello</a>
    <a href="#null" onclick="insert('World')">World</a>
    </form>
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    New Coder
    Join Date
    Apr 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help. It works great, AND I understand how it works

    I never realized how similar javascript is to PHP.


  •  

    Posting Permissions

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