/    Sign up×
Community /Pin to ProfileBookmark

Trouble calling functions…

Hey guys. I’m a first poster here and pretty new to JavaScript.

I’ve been working on some functions and they work been when I’m calling them from the *.html file. But the trouble starts when I add the scr code…

[CODE]<script type=”text/javascript” src=”case.js”></script>[/CODE]

This is my case.js code.

[CODE]function check_input()
{
// Get user input from text field in form id of f
var entry = document.forms[0].textfield.value;

// Compare input and display an appropriate message
if(entry.toLowerCase() == “castle”){
alert(“Accepted”);
{
else alert(“Refused”);}
}
}[/CODE]

And this is my html code…

[code=html]<html>
<head>
<title>Untitled Document</title>
<script type=”text/javascript” src=”case.js”>
</script>
</head>
<body>
<input type=”text”>
<input type=”button” value=”Check Input” onClick=”check_input()”>
</body>
</html>[/code]

I don’t understand what the trouble is :S I mean I’ve tested functions when they’re inside the html file but when I seperate them, it just doesn’t work!

Kind regards, James.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 31.2006 — your elements have no form

<body>

[B]<form>[/B]

<input type="text">

<input type="button" value="Check Input" onClick="check_input()">

[B]</form>[/B]

</body>
Copy linkTweet thisAlerts:
@sinkquickauthorAug 31.2006 — Thanks for the help. But I had already tried that and still no luck ?
Copy linkTweet thisAlerts:
@KorAug 31.2006 — your text field has no name either:

<form>

<input type="text" [B]name="textfield"[/B]>

<input type="button" value="Check Input" onClick="check_input()">

</form>

document.forms[0].textfield.value

means the value of the element with the name="textfield" belonging to the first form (forms[0]) of the document.
Copy linkTweet thisAlerts:
@gogelpotAug 31.2006 — Try naming the form and the input field.
[CODE]
<form name="choiceForm">
<input type="text" name="User">
[/CODE]


Now also change the following in your case.js
[CODE]
var entry = document.choiceForm.User.value;
[/CODE]


I dont know if it will work but it is worth trying.
Copy linkTweet thisAlerts:
@sinkquickauthorAug 31.2006 — It still doesn't work ? I just don't understand it!
Copy linkTweet thisAlerts:
@KorAug 31.2006 — neither do we. can we see the entire codes?
Copy linkTweet thisAlerts:
@sinkquickauthorAug 31.2006 — Sure. Here you go...

case.js:

[CODE]function check_input()
{
// Get user input from text field in form id of f
var entry = document.formcase.textfield.value;

// Compare input and display an appropriate message
if(entry.toLowerCase() == "castle"){
alert("Accepted");
{
else alert("Refused");}
}
}[/CODE]


case.html:

[code=html]<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript" src="case.js">
</script>
</head>
<body>
<form name="formcase">
<input type="text" name="textfield">
<input type="button" value="Check Input" onClick="check_input()">
</form>
</body>
</html>[/code]


Hope this helps.
Copy linkTweet thisAlerts:
@KorAug 31.2006 — take care with the brackets:
<i>
</i>function check_input()
[COLOR=Blue]{[/COLOR]
[COLOR=YellowGreen]// Get user input from text field in form id of f[/COLOR]
var entry = document.formcase.textfield.value;
[COLOR=YellowGreen]// Compare input and display an appropriate message[/COLOR]
if(entry.toLowerCase() == "castle")[COLOR=Red]
{[/COLOR]
alert("Accepted");
[COLOR=Red]}[/COLOR]
else [COLOR=Red]
{[/COLOR]
alert("Refused");
[COLOR=Red]}[/COLOR]
[COLOR=Blue]}[/COLOR]


if(...)[B]{...}[/B]

else if(...)[B]{...}[/B]

else[B]{...}[/B]
×

Success!

Help @sinkquick 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.7,
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,
)...