Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , , , , ,

Sponsored Links



Pass Variables By Reference in Javascript?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 11-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 11-03-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
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);
Reply With Quote
  #3  
Old 11-03-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
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);
Reply With Quote
  #4  
Old 11-03-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
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);
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 11-03-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
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;

}
Reply With Quote
  #6  
Old 11-03-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
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>
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Pass Variables By Reference in Javascript?"
Thread Thread Starter Forum Replies Last Post
How to Pass a Javascript variable to a PHP script leshaspar Software Development 4 24-11-2010 10:48 AM
How to pass reference to vector objects Recko Windows Software 3 07-08-2009 11:23 AM
How to pass variables objShell.Run command Elkanah Software Development 4 06-06-2009 11:06 PM
how do i parse variables in the URL using javascript Gunter Software Development 3 04-06-2009 11:38 PM
What is difference between pass by value & pass by reference in java? Baran Software Development 4 25-02-2009 07:15 PM


All times are GMT +5.5. The time now is 12:11 PM.