/    Sign up×
Community /Pin to ProfileBookmark

Event Handler Question – OnChange

hey guys,
just trying to wrap my head around javascript this week and came across something I need some help with understanding.

lets say I have a simple script:

[code=html]<form name=”jim”>
name: <input type=”text” name=”fname” size=”10″ onChange=”alert(‘hi’);”>

<a href=”#” onClick=”document.jim.fname.value=’jim’;”> CLICK ME! </a>

</form>[/code]

now what that does… when you click on the CLICK ME link it puts the string “jim” in the text box’s value field.

well what I’m trying to figure out is when the string gets the value “jim” or whatever I want the alert box to come up.

so the flow would be: user clicks link, value of text field is now “jim”, since the text field went from blank to now holding “jim” pop up the alert box.

I’ve tried every event type I could find but it doesn’t seem to work. am I missing something? I realize that changing the value doesn’t really mean its an event so to say.

thanks~!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@PittimannDec 07.2003 — Hi!

Your problem is:

The onchange event occurs when a control loses the input focus and its value has been modified since gaining focus. This attribute applies to the following elements: INPUT, SELECT, and TEXTAREA. (taken from: www.w3.org).

That means: if the user types something into the textfield, he has to click elsewhere before the event occurs. Test it - just type something in there and after that click somewhere else. The alert will pop up...

If you want the alert to be displayed when clicking your link, you can do the following:

<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name="jim"&gt;
enter your name here: &lt;input type="text" name="fname" size="10" onChange="alert('Hi, '+document.jim.fname.value+'!');"&gt;
&lt;a href="#" onClick="document.jim.fname.value='jim';alert('hi');"&gt; CLICK ME! &lt;/a&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Cheers - Pit
Copy linkTweet thisAlerts:
@cobain_attacksauthorDec 07.2003 — hey Pittimann, thanks for the reply

my example was really just a simple test of what ideally I'd like to do...

the more true problem is this:

I have my form field called "date" with an image next to it, when you click that image an onclick is called that displays a little pop up calendar. you choose a date from the calendar and the date goes into the box. all that works just fine

the problem comes in where I have several other boxes I want to update with that new date.

so here is the ideal flow...

user clicks image, pop up calendar comes up, user chooses date, date goes into form field, all other input fields get updated with the new date.


now I have everything working, except a way to call my bulk update function when a value gets submitted in that field from another js function.

I dont really want to put the function into the calendar script because I use it for other things as well. and since the image loses focus when the pop up calendar comes up I can't do anything with the image. I'm quite lost on this one ?
×

Success!

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