/    Sign up×
Community /Pin to ProfileBookmark

using onClick, onFocus, and onChange together

Hi everyone,

I am new to JavaScript and have run into a small problem.

My code works perfectly inside DreamWeaver but is crazy in both IE8 and FireFox.

In FireFox, I cannot insert text in the text fields.

In IE8, I can insert text, but nothing else happens.

My code is as follows:

[CODE] <script language=”JavaScript”>
<!–
var Counter;

function initForm()
{
Count = 0; // starts the count at 0
document.form1.timeEvents.value = Count; // displays the count
}

function increaseCount()
{
Count ++; // increases the count by 1 each time it is used
document.form1.timeEvents.value = Count; // displayes the new count
}

function clicked(fieldName)
{
increaseCount(); // calls increaseCount function
alert(fieldName + ” was clicked. Event Count = ” + Count + “.”); // sends alert and displays new count when event is triggered
}

function change(fieldName)
{
increaseCount(); // calls increaseCount function
alert(fieldName + ” was changed. Event Count = ” + Count + “.”); // sends alert and displays new count when event is triggered
}

function withfocus(fieldName)
{
increaseCount(); // calls increaseCount function
alert(fieldName + ” is focused. Event Count = ” + Count + “.”); // sends alert and displays new count when event is triggered
}
//–>
</script>[/CODE]

[CODE]
<form id=”form1″ name=”form1″ method=”post” action=””>

<label>First Name
<input name=”FirstName” type=”text” onClick=”clicked(document.form1.FirstName.name);” // calls clicked function when FirstName field is clicked
onChange =”change(document.form1.FirstName.name);” // uses the change fuction when FirstName field has been altered
onFocus =”withfocus(document.form1.FirstName.name);” // When FirstName has focus, calls the withfocus function
/>

</label>
<br />

<label>
Last Name</label>
<label>
<input name=”LastName “type=”text” id=”LastName” onClick=”clicked(document.form1.LastName.name);” // calls clicked function when LastName filed is clicked
onChange =”change(document.form1.LastName.name);” //calls change function when LastName field detects a change
onFocus =”withfocus(document.form1.LastName.name);” // when focus is on LastName field, calls withfocus function
/>
</label>

<br />

<label>Phone Number
<input name=”Phone” type=”text” id=”Phone” onClick=”clicked(document.form1.Phone.name);” // calls the click function when phone field has been clicked
onChange =”change(document.form1.Phone.name);” // calls the change function when a change occurs in the phone field
onFocus =”withfocus(document.form1.Phone.name);” // calls the withfocus function when focus is on the phone filed
/>
</label>

<br />
Number of times event(s) was triggered
<input type=”text” name=”timeEvents” />

[/CODE]

Any suggestions would be greatly appreciated

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@futurecoder12authorSep 13.2011 — Any help would be appreciated. ?
Copy linkTweet thisAlerts:
@yasashi12Sep 14.2011 — Good job but is it really necessary to create alerts when you click the fields? if not, try removing it. I guess, that creates the error.
Copy linkTweet thisAlerts:
@MouryaSep 14.2011 — hi ,

i changed the function names(onchange,onfocus,onclick) , now all are lower case letters and removed the comments.

also included var count=0; this acts like a global variable and can be accessed inside all the functions.

and i think u forgot to specify id="FirstName" for the first input element.

[CODE]
<html>
<head>

<script type="text/javascript">

[B]var Count=0;[/B]
var Counter;

function initForm()
{
Count = 0; // starts the count at 0
document.form1.timeEvents.value = Count; // displays the count
}


function increaseCount()
{
Count ++; // increases the count by 1 each time it is used
document.form1.timeEvents.value = Count; // displayes the new count
}


function clicked(fieldName)
{
increaseCount(); // calls increaseCount function
alert(fieldName + " was clicked. Event Count = " + Count + "."); // sends alert and displays new count when event is triggered
}

function change(fieldName)
{
increaseCount(); // calls increaseCount function
alert(fieldName + " was changed. Event Count = " + Count + "."); // sends alert and displays new count when event is triggered
}

function withfocus(fieldName)
{
increaseCount(); // calls increaseCount function
alert(fieldName + " is focused. Event Count = " + Count + "."); // sends alert and displays new count when event is triggered
}

</script>

</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>
First Name
<input name="FirstName" [B]id="FirstName"[/B] type="text" onclick="clicked(document.form1.FirstName.name);"
onchange="change(document.form1.FirstName.name);" onfocus="withfocus(document.form1.FirstName.name);" />
</label>
<br />
<label>
Last Name</label>
<label>
<input name="LastName " type="text" id="LastName" onclick="clicked(document.form1.LastName.name);"
onchange="change(document.form1.LastName.name);" onfocus="withfocus(document.form1.LastName.name);" />
</label>
<br />
<label>
Phone Number
<input name="Phone" type="text" id="Phone" onclick="clicked(document.form1.Phone.name);"
onchange="change(document.form1.Phone.name);" onfocus="withfocus(document.form1.Phone.name);" />
</label>
<br />
Number of times event(s) was triggered
<input type="text" name="timeEvents" />
</form>
</body>
</html>
[/CODE]



Thanks

Mourya
Copy linkTweet thisAlerts:
@futurecoder12authorSep 15.2011 — Thanks a million!
Copy linkTweet thisAlerts:
@MouryaSep 16.2011 — Happy coding ?
×

Success!

Help @futurecoder12 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...