Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-26-2010, 08:42 AM   PM User | #1
aalexaa
New Coder

 
Join Date: Jan 2010
Location: california
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
aalexaa is an unknown quantity at this point
Unhappy manipulating input values

I have the following code:

Code:
<html>
<head>
<script type="text/javascript">
function copyValue()
{
var x= document.getElementById("fname").value;
var y =document.getElementById("dname").value;
x = y;
}
</script>
</head>

<body>
<input type="text" id="dname" onChange="copyValue()" />
Enter your name: <input type="text" id="fname" />
</body>

</html>
and it won't work.
but when i add an extra line, alert(x+" "+y) to tell me what the value of each input is, both values are the same. but the text won't show up in the second input field.

my goal here is to have the second input field change to whatever the first one says after i've typed something.
any suggestions?
aalexaa is offline   Reply With Quote
Old 03-26-2010, 10:11 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by aalexaa View Post
and it won't work.
but when i add an extra line, alert(x+" "+y) to tell me what the value of each input is, both values are the same. but the text won't show up in the second input field.
well, it does work, just not the way you think it works.

in general, everything besides the primitives is passed by reference*, thus in the beginning x and y point to the two input’s value attributes. when you do x = y x points now to the same value attribute as y, without doing anything to the previous assigned value.
PHP Code:
function copyValue()
{
    var 
document.getElementById("fname");
    var 
document.getElementById("dname");
    
x.value y.value;

* - can be annoying when working with arrays
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 03-26-2010 at 10:16 AM..
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
aalexaa (03-27-2010)
Old 03-26-2010, 12:39 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
Originally Posted by Dormilich View Post
* - can be annoying when working with arrays
How's that?
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 03-26-2010, 01:00 PM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
example
PHP Code:
// …
    
do {
        
values this.scramble(arr);
    }
    while (
this.unsolvable(values));
// …


MagicSquare.prototype.scramble = function (a)
{
    var 
iindex= [];

// if I don’t hardcopy the array here, the array 
// in the calling function (arr) becomes [] and thus
// unuseable in the next cycle (if any)
    
for (a.lengthi--;) {
        
c[i] = a[i];
    }
    for (
c.lengthi--;) {
        
index Math.round(Math.random() * 100 i) % i;
        
b.push(c.splice(index1)[0]);
    }
    return 
b;
}; 
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 03-26-2010 at 01:06 PM..
Dormilich is offline   Reply With Quote
Old 03-27-2010, 08:20 AM   PM User | #5
aalexaa
New Coder

 
Join Date: Jan 2010
Location: california
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
aalexaa is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
well, it does work, just not the way you think it works.

in general, everything besides the primitives is passed by reference*, thus in the beginning x and y point to the two input’s value attributes. when you do x = y x points now to the same value attribute as y, without doing anything to the previous assigned value.
PHP Code:
function copyValue()
{
    var 
document.getElementById("fname");
    var 
document.getElementById("dname");
    
x.value y.value;

* - can be annoying when working with arrays
wow it worked! thank you.

who knew it was such a simple mistake...
aalexaa is offline   Reply With Quote
Old 03-27-2010, 09:54 AM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by aalexaa View Post
who knew it was such a simple mistake...
well, it is always that simple, once you got the hang of it …
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Tags
dom, input, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:41 PM.


Advertisement
Log in to turn off these ads.