iEntry 10th Anniversary CSS Snippets JavaScript Snippets

 
 





Passing Variables to Functions

Passing variables to functions if very important in programming. This allows the programmer to reuse code that they have created multiple times. Because the programmer can use the code multiple times, it also makes the code easier to edit if needed. In this script we will be passing the information that a user enters to a function, and which will change some text on the page.

<script language="JavaScript" type="text/javascript">
<!--
function pass(InputValue){
	document.getElementById('output').innerHTML = "Hello " + InputValue + "!!!!";
}	
//-->
</script>
Hello

As you can see we created a function called pass(), and it only does one thing, and that is change the text from Hello. How we passed the data to the variable is by including it in the parenthesises when we call the function, in the code above we passed the value that the user put in the text box named Input in the form name first. We can only do this if the function know that we are going the pass a variable to it, that is why our function has text between it's parenthesises. If the function does not know that you are passing a variable to it, then the function will not do what you want it to do. When in the function, you will call this variable by the name you give it when creating the function, in the example this is InputValue.