"Button event code" to get data from field1 and put it in filed2. when button clicked

rated by 0 users
This post has 4 Replies | 2 Followers

Top 10 Contributor
Posts 109
Points 1,425
Christoffer Stub Michelsen Posted: Wed, Aug 25 2010 5:48 AM

when i a method for a "button onClick"

what should I write to get text from 1 box to another. ?

where can I find a definition for these window.  calls ?

thanks

Christoffer

  • | Post Points: 20
Top 10 Contributor
Posts 389
Points 5,815

Hi Christoffer,

I don't know if you figured this one out yet. It was nagging at me because it should be easy.

After some effort I get the following:

IN the onClick of the button:

Assuming you have two text fields called "text0" and "text1"

 

var myTxt0 = document.getElementsByName("text0");

var myTxt1 = document.getElementsByName("text1");

 var myVal01 = myTxt0[0].value;

var myVal00 = myTxt1[0].value;

myTxt1[0].value = myVal01;

myTxt0[0].value = myVal00;

 return true;

 

This swaps the values in the fields.

I'm sure there are more "correct" ways of doing this but I couldn't figure them out.

The obvious "myTxt0.getAttribute("value") is not supported by the element returned in the document.getElementByName("text0").

Anyway. If this helps you then that's great. If not it has been a useful excercise for me.

Cheers,

Brian.

  • | Post Points: 5
Top 10 Contributor
Posts 389
Points 5,815

You can get a full collection of all of the "input" elements on the form using this:

var elements = document.getElementsByTagName("input");

var valArray = new Array();

var valArray2 = new Array();

var nameArray = new Array();

for (var j=0;j<elements.length;j++)

{

    var fld = elements[j];

    nameArray[j] = elements[j].name;

    valArray[j] = fld.value;

    valArray2[j] = elements[j].value;

}

Then once you have all the elements you can do things with them in a more "normal" fashion by looping though the array to find the input field you want and working with it.

Cheers,

Brian.

  • | Post Points: 20
Top 10 Contributor
Posts 109
Points 1,425

Brian

Thanks for your answer, it works perfekly for me.

do you know if there i a way to do the same in c# or is it only possible to run c# on the server-side

Christoffer

  • | Post Points: 20
Top 10 Contributor
Posts 389
Points 5,815

Christoffer,

C# and VB are server side programming tools only.

Javascript is the Client side programming language.

Cheers,

Brian.

  • | Post Points: 5
Page 1 of 1 (5 items) | RSS