Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    california
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    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
    Last edited by Dormilich; 03-26-2010 at 10:16 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    aalexaa (03-27-2010)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Dormilich View Post
    * - can be annoying when working with arrays
    How's that?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    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;
    }; 
    Last edited by Dormilich; 03-26-2010 at 01:06 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    New Coder
    Join Date
    Jan 2010
    Location
    california
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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...

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    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 …
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •