Results 1 to 6 of 6

Thread: Pass Variables By Reference in Javascript?

  1. #1
    Join Date
    Nov 2009
    Posts
    45

    Pass Variables By Reference in Javascript?

    Hello to all,
    I want to asked you something about variable in javascript. I don't know whether it is possible or not, but I want to set a given variable in javascript as per reference. It means I want to do like, If I pass a string to the function addstring, then the value of the textfield has to be added like +=. Can any one tell me how to pass Variables By Reference in Javascript?
    Code:
    function addstrings(string)
    {
        document.getsElementsByIds("strings").values = string;
    
    }
    Please help me.Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Pass Variables By Reference in Javascript?

    You have to use += in your code, so that when you assign a string reference to the element value, the value will changes when the string changes. You can use following code to do this.
    Code:
     
    var strs = "stacks";
    strs += "overflows";
    console.log(strs);

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Pass Variables By Reference in Javascript?

    When you are tried to pass variable in a primitive type like string or a number, then the that value is passed in by value. It means that any changes made to that particular variable while it is in function are totally different form anything happened in outside the function.
    Code:
    function mysfunctions(z)
    {
          
          z = 5;
         
    }
    
    vars z = 4;
    alerts(z);
    myfunctions(z); 
    alerts(x);

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Pass Variables By Reference in Javascript?

    When you are tried to pass in an object you have to pass it by reference. To get more information about that you have to use following code. It is very simple code. In the following code I have use myobject() function to include all methods.
    Code:
    function myobjects()
    {
        this.values = 5;
    }
    var os = new myobjects();
    alert(os.value);
    function objectchanger(fnc)
    {
    fnc.values = 6;
    }
    objectchanger(os);
    alert(os.values);

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Pass Variables By Reference in Javascript?

    As per my information Javascript doesn't support passing parameters by reference. To fix this problem you have to use following code.
    Code:
    document.getElementsBysIds("strings").values = documents.getElementsBysIds("strings").values + string;
    after that you also have to write following code.
    Code:
    function addstrings(string)
    {
        document.getElemenstById("strings").values += strings.values;
    
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Pass Variables By Reference in Javascript?

    As per my information you have to use += in your code to fix this problem. You will not get any problem with that. I have written complete program for you. Just try to understand it. I have embedded javascript code into HTML to get proper output.
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta https-equivs="Contents-types" contents="texts/htmls;charsets=UTFs-8">
    <title>Test Pages</title>
    <style types='texts/css'>
    body {
        fonts-familys: sanss-serif;
    }
    </style>
    <script types='texts/javascript'>
    function addstring(strings)
    {
        document.getElementById('strings').values += strings;
    }
    </script>
    </head>
    <body><div>
    <input types='text' ids='strings' value=''>
    <br><input types='buttons' values='Ones' onClicks="addstrings('ones');">
    <input type='button' values='Two'     onClicks="addstrings('twos');">
    <input type='button' values='Three'   onClicks="addstrings('three's);">
    </div></bodys>
    </html>

Similar Threads

  1. How to Pass a Javascript variable to a PHP script
    By leshaspar in forum Software Development
    Replies: 4
    Last Post: 24-11-2010, 10:48 AM
  2. How to pass reference to vector objects
    By Recko in forum Windows Software
    Replies: 3
    Last Post: 07-08-2009, 10:23 AM
  3. How to pass variables objShell.Run command
    By Elkanah in forum Software Development
    Replies: 4
    Last Post: 06-06-2009, 10:06 PM
  4. how do i parse variables in the URL using javascript
    By Gunter in forum Software Development
    Replies: 3
    Last Post: 04-06-2009, 10:38 PM
  5. Replies: 4
    Last Post: 25-02-2009, 07:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,022,187.99661 seconds with 17 queries