Announcement

Collapse
No announcement yet.

HTML Form - exchange text field content

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • HTML Form - exchange text field content

    Hiya,

    I was wondering if there was a Javascript somewhere which allowed the function to perform the following:

    A form contains:


    When >> is pressed, the data from both fields are exchanged. Like so:


    Thanks,

    Andy
    Last edited by AndyBoot; 8th May 2006, 12:51.

  • #2
    Re: HTML Form - exchange text field content

    Not good code as I'm away from any JS references, but the syntax is:
    Assume form fields are field1 & field2

    1) varTemp = field1.text;
    2) field1.text = field2.text;
    3) field2.text = varTemp;

    Hope this helps

    Tom
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: HTML Form - exchange text field content

      Hi Ossian,

      Thanks for the syntax, however I don't know much JScript.
      I posted this message on more than 1 forum and thankfully, I got a piece of code which does the job very nicely!

      Code:
      <form name="Form1">
      <script type="text/javascript">
      
      function switchVals(tA, tB){
      	var eleA;
      	var eleB;
      	var val;
      
      	if (document.getElementById){
      		eleA = document.getElementById(tA);
      		eleB = document.getElementById(tB);
      	} else if (document.all) {
      		eleA = document.all[tA];
      		eleB = document.all[tB];
      	} else if (document.layers) {
      		eleA = document.layers[tA];
      		eleB = document.layers[tB];
      	}
      	val = eleA.value;
      	eleA.value = eleB.value;
      	eleB.value = val;
      }
      </script>
      
      <INPUT TYPE="text" value="Datao1a" id="tA1">
      <INPUT TYPE="button" value=">>" onclick="javascript: switchVals('tA1', 'tA2');">
      <INPUT TYPE="text" value="Data02B" id="tA2">
      
      </form>

      Comment

      Working...
      X