/    Sign up×
Community /Pin to ProfileBookmark

Calling function of js in separate file

<code>I am trying to call a js function from an html page.

<html>

<head>

<script type=”text/javascript” src=”regex.js”></script>

</head>

<body>

<form onchange=”my_function(name)”>

<input type=”text” name=”fn”>

</form>

</body>

</html></code>

is this correct?
is there any problem with the parameter passing?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledApr 10.2009 — what is supposed to be the [b]name[/b] parameter passed to function [b]my_function[/b]? also, it seem that you want to call the function when any of the form's field change of value. so the reason you defined the event [b]onchange[/b] in the form element. i hasn't done that before but should work. however, it is handled a bit different. onchange capture an event object. can you clarify if this is your intention?
Copy linkTweet thisAlerts:
@nishkarshsauthorApr 10.2009 — i want to pass the string in the text field as a parameter. should that be "[B]fn[/B]" instead of "[B]name[/B]". also plz check the format of the way the function is called.

[B]my_function[/B] is a function in my js file which is separately stored (in the same folder)
Copy linkTweet thisAlerts:
@ZeroKilledApr 10.2009 — my_function is a function in my js file which is separately stored (in the same folder)[/quote] if i correctly understood your doubt, it don't matter where is defined, either on external file or in the same document. most important is that the function exists.

i want to pass the string in the text field as a parameter. should that be "fn" instead of "name".[/quote]
problem here is that if you want to pass the string of the field, you have to use the event object that traverse in your document because the handler is registered on another element. if this is too complex for you to understand, then the easier solution would be to move the handler to correct element, in your case the input field.
<i>
</i>&lt;form&gt;
&lt;input type="text" name="fn" onchange="my_function(this.value);" /&gt;
Copy linkTweet thisAlerts:
@nishkarshsauthorApr 11.2009 — i made the changes as per you asked. when i open the html file in the browser it displays the html code instead of the field..

[CODE]<html>
<head>
<script type="text/javascript" src="regex.js"></script>
</head>
<body>
<form>
<input type="text" name="fn" onchange="my_function(this.value)">
</form>
</body>

</html>[/CODE]


the js file is:
[CODE]
function my_function(st) {
var p1=new RegExp("nishkarsh");
document.write(p1.test(st));
}[/CODE]


and the code is for text field validation (ie if the entered text is same as "nishkarsh"

plz help me out...
Copy linkTweet thisAlerts:
@nishkarshsauthorApr 11.2009 — also will the above code work dynamically... ie will it check each time user enters a character (changes the text field) and display the name to be valid if it matches.
×

Success!

Help @nishkarshs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...