/    Sign up×
Community /Pin to ProfileBookmark

Output text from separate javascript to form

Hi,

I am writing a form validation script. I am new to javascript and from what I have read, it’s best practice to put your javascript in a separate file from your HTML.

If my validation script detects that a field is empty, I want it to output a warning message right above the form fields on the webpage. I have the form call a function using onSubmit. When the script uses document.write, it is putting the warning on a new webpage instead of putting the warning above the form itself.

Is it even possible to do this or in this case will I need to keep the javascript within the same file as the form (.htm in this case). I have done some searching but haven’t found anything useful so far.

I know I have seen this done on websites before but I don’t know if they are using a separate javascript file or if it’s embedded in the form webpage. I never actually looked at the time.

Thanks for your help!
Rob

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 19.2010 — As you have seen, using [I]document.write[/I] after the page has loaded creates a new document.

The error text can be written to an empty element or an element is created dynamically or a hidden element is moved to the correct position an made visible.
Copy linkTweet thisAlerts:
@robmoauthorSep 19.2010 — Fang,

Thanks for your suggestion! I will research this further and decide which method achieves what I am striving to do. I'll take a close look at your links as well.

Thanks again!

Rob
Copy linkTweet thisAlerts:
@robmoauthorSep 21.2010 — Is it possible to put a hidden error message on a webpage and use javascript to toggle in and out of view? I might be in over my head here. I'm still looking into the innerHTML property.
×

Success!

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