By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,814 Members | 2,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,814 IT Pros & Developers. It's quick & easy.

show value of textfield

P: 89
Hello All

I have a script where i need to show the value of a text field in another textfield/field. I have done the same with my radio buttons and select fields like this:

[HTML]<script type="text/javascript">
function check(layers){
document.forms[0].answer.value=layers
}

function PrintValues(){

var myString = document.mainform.file.value
var myStringLength = myString.length
var Comma = myString.lastIndexOf(',')
var SufNumChars = Comma + 1

document.mainform.result.value=('');

for(i=0; i<Comma; i++)
document.mainform.result.value+=(myString.charAt(i ));

document.mainform.result.value+=(' ');

for(i=SufNumChars; i<myStringLength; i++)
document.mainform.result.value+=(myString.charAt(i ));

}

</script>

<div class="AccordionPanelContent">
<input name="layers" value="2" type="radio" onclick="check(this.value)" /> 2<br />
<input name="layers" type="radio" onclick="check(this.value)" /> 4<br />
<input name="layers" type="radio" onclick="check(this.value)" value="6" /> 6<br />
<input name="layers" type="radio" onclick="check(this.value)" value="8" /> 8
</div>
<input type="text" name="answer" size="5" readonly="readonly">
<div class="AccordionPanelContent">
<select name="file" size="1" onchange="PrintValues();" target="_parent._top"
onmouseclick="this.focus()">
<option value="DHL">Ship with DHL</option>

<option value="UPS">Ship with UPS</option>
</select>

<input name="result">
</div>
[/HTML]
But i can't seem to figure out how to do this with a textfield. Can anybody help me with this...???

Best regards
Aug 5 '08 #1
Share this Question
Share on Google+
22 Replies


RamananKalirajan
100+
P: 607
Hi Dude, I am sorry I am not able to follow your requirement clearly, but I had made some modification in ur code and it's working. Just check it out and post back if u need any change.

[HTML]<html>
<head>
<script type="text/javascript">
var resVal;
function check(layer)
{
resVal=layer;
// alert(resVal);
}
function printThis()
{
var result;
result= resVal;
result+=","+document.getElementById('myAnswer').va lue;
var x = document.getElementById('myFile').selectedIndex;
result+=","+document.getElementById('myFile').opti ons[x].text;
document.getElementById('myResult').value=result;
}
</script>
</head><body>
<form>
<input type="radio" name="layers" onclick="check(this.value)"
value="2">2<br />
<input type="radio" name="layers" onclick="check(this.value)"
value="4">4<br />
<input type="radio" name="layers" onclick="check(this.value)"
value="6">6<br />
<input type="radio" name="layers" onclick="check(this.value)"
value="8">8<br />
<br />
<input type="text" id="myAnswer">
<br/>
<select id="myFile">
<option>Ship with DHL</option>
<option>Ship with UPS</option>
</select>
<br/>
<input type="text" id="myResult" onclick="printThis()">
</form>
</body>
</html>[/HTML]

Regards
Ramanan Kalirajan
Aug 5 '08 #2

P: 89
Works great, thanks, but what i really needed was a modification of the javascript, so that when i use a textfield in the form, the input of the textfield would also show in another textfield or <td> tag.

Maybe be using getelementbyid or something like that, but can't quit figure out how to do that......
Aug 5 '08 #3

RamananKalirajan
100+
P: 607
Works great, thanks, but what i really needed was a modification of the javascript, so that when i use a textfield in the form, the input of the textfield would also show in another textfield or <td> tag.

Maybe be using getelementbyid or something like that, but can't quit figure out how to do that......
Hi dude, If u enter some characters in a textfield the same must be copied into another textfield. whether this is your requirement?

Regards
Ramanan Kalirajan
Aug 5 '08 #4

acoder
Expert Mod 15k+
P: 16,027
printline, as a full member now, you should know that we expect your code to be posted in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use the tags in future. I don't want to have to tell you again.

MODERATOR.
Aug 5 '08 #5

P: 89
To RamananKalirajan

Yes, exactly what i need, but how do i do that....???
Aug 5 '08 #6

acoder
Expert Mod 15k+
P: 16,027
Try something like:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("IDofTextFieldToBeCopiedTo").value = document.getElementById("IDofTextFieldToBeCopiedFrom").value;
Aug 5 '08 #7

RamananKalirajan
100+
P: 607
hi Dude, use this code.

[HTML]<html>
<head>
<script language="javascript">
function printThis()
{
document.getElementById('myText2').value=document. getElementById('myText1').value;
}
</script>
</head>
<body>
Entered Text:<input type="text" id="myText1" onchange="printThis()">
<br/>
Copied Text: <input type="text" id="myText2">
</body>
</html>[/HTML]

Pls be careful of those white spaces while u copy the code.
Any Probs post back it.

Regards
Ramanan Kalirajan
Aug 6 '08 #8

acoder
Expert Mod 15k+
P: 16,027
[HTML]<script language="javascript">[/HTML]
Don't use the language attribute. It's deprecated. Use the type attribute instead:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
Aug 6 '08 #9

RamananKalirajan
100+
P: 607
Don't use the language attribute. It's deprecated. Use the type attribute instead:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
Ok Mr. Acoder here after I won't repeat the same mistake

Regards
Ramanan Kalirajan
Aug 6 '08 #10

acoder
Expert Mod 15k+
P: 16,027
No problem. Here's a link to the W3C documentation if you're interested.
Aug 6 '08 #11

RamananKalirajan
100+
P: 607
No problem. Here's a link to the W3C documentation if you're interested.

hi Acoder, thank's for the link. It will be really helpful for me.

Regards
Ramanan Kalirajan
Aug 6 '08 #12

P: 89
Hello

All of your code works great, but i have run into a problem with my form. I have a field called total, where the value of the field is automatically generated from a script. I don't change this field manually.

Now i want to copy this field to another field by doing this:

Expand|Select|Wrap|Line Numbers
  1. <script language="text/javascript">
  2. function printThis7()
  3. {
  4.    document.getElementById('total2').value = document.getElementById('total').value; 
  5. }
  6. </script>
[HTML]<input type="text" onchange="printThis7()" id="total" value="" name="total" size="10" readonly="readonly">[/HTML]

and

[HTML]<input type="text" id="total2" value="" name="total2" size="10" readonly="readonly">[/HTML]

but it doesn't copy the value...?

Is this because the value of the total field is automatically generated...? if so, how do i then copy the value...?
Aug 11 '08 #13

RamananKalirajan
100+
P: 607
Hello

All of your code works great, but i have run into a problem with my form. I have a field called total, where the value of the field is automatically generated from a script. I don't change this field manually.

Now i want to copy this field to another field by doing this:

Expand|Select|Wrap|Line Numbers
  1. <script language="text/javascript">
  2. function printThis7()
  3. {
  4.    document.getElementById('total2').value = document.getElementById('total').value; 
  5. }
  6. </script>
[HTML]<input type="text" onchange="printThis7()" id="total" value="" name="total" size="10" readonly="readonly">[/HTML]

and

[HTML]<input type="text" id="total2" value="" name="total2" size="10" readonly="readonly">[/HTML]

but it doesn't copy the value...?

Is this because the value of the total field is automatically generated...? if so, how do i then copy the value...?
The reason why its not copied is on some of the browser onChange does't works properly. That's the reason. Don't worry about ur code its ok. Just change the onchange event to some other event like onBlur or onFocus

Regards
Ramanan Kalirajan
Aug 11 '08 #14

P: 9
[Reply=Nan Averaa]
Hello Frnd,
I'll small change your coding. Pls try again. All the best.
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function check(layers){
  3.         document.forms[0].answer.value=layers
  4.     }
  5.  
  6. function PrintValues(){
  7.  
  8.   var myString = document.mainform.file.value
  9.   var myStringLength = myString.length
  10.   var Comma = myString.lastIndexOf(',')
  11.   var SufNumChars = Comma + 1
  12.      alert(myString);//+"<br>"+myStringLength+"<br>"+Comma+"<br>"+SufNumChars);
  13.   document.mainform.result.value=('');
  14.  
  15.   for(i=0; i<Comma; i++) 
  16.   document.mainform.result.value+=(myString.charAt(i  ));
  17.  
  18.   document.mainform.result.value+=(' ');
  19.  
  20.   for(i=SufNumChars; i<myStringLength; i++) 
  21.   document.mainform.result.value+=(myString.charAt(i  ));
  22.  
  23. }
  24.  
  25. </script>
  26.  <form name="mainform" >
  27. <div class="AccordionPanelContent">
  28.         <input name="layers" value="2" type="radio" onclick="check(this.value)"  /> 2<br />
  29.         <input name="layers" type="radio" onclick="check(this.value)" /> 4<br />
  30.         <input name="layers" type="radio" onclick="check(this.value)" value="6" /> 6<br />
  31.         <input name="layers" type="radio" onclick="check(this.value)" value="8" /> 8
  32.     </div>
  33. <input type="text" name="answer" size="5" readonly="readonly">
  34. <div class="AccordionPanelContent">
  35. <select name="file" size="1" onchange="PrintValues();"  target="_parent._top"
  36.         onmouseclick="this.focus()">
  37.   <option value="DHL">Ship with DHL</option>
  38.  
  39.   <option value="UPS">Ship with UPS</option>
  40. </select>
  41.  
  42. <input name="result">
  43. </div>
  44. </form>
  45.  
[/Reply]


The reason why its not copied is on some of the browser onChange does't works properly. That's the reason. Don't worry about ur code its ok. Just change the onchange event to some other event like onBlur or onFocus

Regards
Ramanan Kalirajan
Aug 11 '08 #15

acoder
Expert Mod 15k+
P: 16,027
I have a field called total, where the value of the field is automatically generated from a script. I don't change this field manually.

Now i want to copy this field to another field by doing this:

...
but it doesn't copy the value...?

Is this because the value of the total field is automatically generated...? if so, how do i then copy the value...?
Yes, onchange won't fire because the field is readonly and hasn't been changed by the user when they remove focus. The simple solution is to add one extra line to the script which changes the value of the first to also change the value of the second total field.
Aug 11 '08 #16

P: 89
Hi'

Could you give me an example on how to do this...???
Aug 12 '08 #17

acoder
Expert Mod 15k+
P: 16,027
Show the code that changes the total field.
Aug 12 '08 #18

RamananKalirajan
100+
P: 607
Hi'

Could you give me an example on how to do this...???
Hi Mr. Printline, this is a sample code

[HTML]<html>
<head>
<script type="text/javascript">
function calculateMe()
{
var a = document.getElementById('myText1').value;
var b = document.getElementById('myText2').value;
var res = a*b;
document.getElementById('resText1').value=res;
document.getElementById('resText2').value=document .getElementById('resText1').value
}
</script>
</head>
<body>
Enter a Number: <input type="text" id="myText1"><br/>
Enter b Number: <input type="text" id="myText2"><br/>
<input type="button" value="calculate" onclick="calculateMe()"><br/>
Result: <input type="text" id="resText1"><br/>
Copied: <input type="text" id="resText2"><br/>
</body>
</html>[/HTML]

Still confused, post ur code. I will help u out

Regards
Ramanan Kalirajan
Aug 12 '08 #19

P: 89
Hello RamananKalirajan

My code looks like this:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function MM_findObj(n, d) { //v4.01
  3.   var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  4.     d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  5.   if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  6.   for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  7.   if(!x && d.getElementById) x=d.getElementById(n); return x;
  8. }
  9.  
  10. function KW_getVal(o){ //v1.2
  11.     var retVal="0";if (o.type=="select-one")
  12.     {retVal=(o.selectedIndex==-1)?0:o.options[o.selectedIndex].value;}
  13.     else if (o.length>1){for (var i=0;i<o.length;i++) if (o[i].checked) retVal=o[i].value;
  14.     } else if (o.type=="checkbox") {retVal=(o.checked)?o.value:0;} else {
  15.     retVal=Number(o.value)}return parseFloat(retVal);
  16. }
  17.  
  18. function KW_calcForm() { //v1.2
  19.     var str="",a=KW_calcForm.arguments; for (var i=3;i<a.length;i++)
  20.     str+=(a[i].indexOf("#")==-1)?a[i]:KW_getVal(MM_findObj(a[i].substring(1)));
  21.     t=Math.round(a[1]*eval(str))/a[1];tS=t.toString();if(a[2]>0){tSp=tS.indexOf(".");
  22.     if(tSp==-1)    tS+=".";tSp=tS.indexOf(".");while(tSp!=(tS.length-1-a[2])){tS+="0";
  23.     tSp=tS.indexOf(".");}} MM_findObj(a[0]).value=tS;
  24. }
  25.  
  26. function printThis7()
  27. {
  28.    document.getElementById('total2').value = document.getElementById('total').value; 
  29. }
  30. </script>


[HTML]<input type="text" onchange="printThis7()" value="" name="total" size="10" readonly="readonly">[/HTML]

And this is how the total is calculated (The onblur event):

[HTML]<input name="quantity" type="text" onchange=" onblur="KW_calcForm('total',100,2,'(','(','#quanti ty','*','#priceperpiece',')','+','#shipping',')',' +','#vat')" value="" size="30" />[/HTML]

Does this makes any sence to you....?

My "function printThis7()" doesn't copy the field.
Aug 14 '08 #20

acoder
Expert Mod 15k+
P: 16,027
That's because onchange doesn't fire. Try this:
[HTML]<input name="quantity" type="text" onblur="KW_calcForm('total',100,2,'(','(','#quanti ty','*','#priceperpiece',')','+','#shipping',')',' +','#vat'); printThis7();" value="" size="30" />[/HTML]
Aug 14 '08 #21

P: 89
Thanks a lot. That helped!
Aug 14 '08 #22

acoder
Expert Mod 15k+
P: 16,027
You're welcome :)
Aug 14 '08 #23

Post your reply

Sign in to post your reply or Sign up for a free account.