Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a JavaScript file and a HTML file. This html file contains a form. What I want is to create a JavaScript function that accepts the data from the HTML pages' textboxes and assign those to the JS function(). Remember that this JS function is in a different JS file. So how can I do that. I already used JavaScript global variables. So can someone help meto solve this matter?

Thanks & regards,
Chiranthaka
Posted
Comments
AnvilRanger 17-Jul-14 8:25am    
What exactly are you trying to accomplish? Just at the reference to your js in the html document and then bind your function to some event. You can then pass any data you need to as parameters to the function.
Chiranthaka Sampath 17-Jul-14 11:54am    
By referencing the JS file in the HTML file does it acts by directionally?
AnvilRanger 17-Jul-14 13:06pm    
What do you mean "by directionally"? When you add the js reference to the HTML document that is the link. You are able to call the js functions from the elements in the HTML document and you have access to the HTML elements in js.
Chiranthaka Sampath 17-Jul-14 13:18pm    
If I enter the data to the form then show be how to pass them to the external JS files' variables.
Chiranthaka Sampath 17-Jul-14 13:53pm    
This is my HTML Page with the form.

FirstPage.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="ExternalJS.js"></script>
<title>First Page</title>
</head>
<body>
<!-- Insert your content here -->
<form name="textform" action="" method="get">

Text One: <input type="text" id="textone" name="textone" />
<input type="submit" value="Submit" önClick=getVar();false />

</form>

</body>
</html>


This is the external JavaScript File.

//var strTextOne = document.getElementById("textone").value;
var strTextOne = document.textform.textone.value;

function getVar(){

alert(strTextOne);

}

I want to pass the data entered to the HTML textbox to the JavaScript Function() in the external JavaScript File. Now I think you can understand what you need.

1 solution

Ok I found the solution & it's below.

The HTML Page With The Form





<title>First Page




Text One:








The External JavaScript File


function getVar(){
var strTextOne = document.getElementById("textone").value;
alert(strTextOne)
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900