38 users online (0 members and 38 guests)  


  Results 1 to 2 of 2

Related

  1. Replies: 6
  2. image swap    Forum: CSS Forum
    Replies: 1
  3. mouse swap?    Forum: HTML Forum
    Replies: 1
  1. #1
    shaunbrazier's Avatar
    New User

    Status
    Offline
    Join Date
    Jan 2005
    Posts
    1

    Swap textarea values

    What would be the best way to swap the contents of two text areas?

    I have the following attached to a text link:

    onclick="document.forms[0].heading1.value=document.forms[0].heading2.value;document.forms[0].heading2.value=document.forms[0].heading1.value"

    But it only changes heading1 to the value of heading2. How can I swap them?

    Many thanks.

  2. #2
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732
    Hi there shaunbrazier,

    and a warm welcome to these forums

    Actually, it is working

    This is what is taking place....
    Let's call the two values...'a' and 'b'
    onclick replaces value 'a' with that of 'b'
    value 'a' now no longer exists so value 'b'
    is then replaced with.......value 'b'.

    To overcome this problem this value 'a'
    needs to be stored prior to the first swap
    and it can then be called for the second.

    Here is the script that does this, it employs another variable to store value 'a'.
    Code:
    <script type="text/javascript">
    <!--
    
    function  swapAreaText() {
    
      var df=document.forms[0];
    
      var area1=df[0].value;
      var area2=df[1].value;
      var store=area1;
      
      df[0].value=area2;
      df[1].value=store;
    
    //-->
     }
    </script>
    
    <form action="#">
    <div>
    <textarea rows="3" cols="13">this is textarea one well it is until the link is clicked</textarea>
    <textarea rows="3" cols="13">and this is textarea two</textarea>
    </div>
    </form>
    
    <a href="javascript:void(swapAreaText())">this is a link</a>
    coothead



Tags for this Thread