/    Sign up×
Community /Pin to ProfileBookmark

How to deal with missing ellements?

I just found the problem which have been breaking my JavaScript all the time!

The function initial() fails the entire (well, most of it) if not all elements/IDs are available on the pages it loads with. Something which do not occur on all my pages.

Is it possible to rewrite this function so that it allows the script to work even if not all ellements are avaialble at all times?

[code]function initial() {
if (document.getElementById(‘s’).value == ”) {
document.getElementById(‘s’).value = ‘Search…’;
}
document.getElementById(‘s’).onfocus = searchFieldFocus;
document.getElementById(‘s’).onblur = searchFieldBlur;

if (document.getElementById(’email’).value == ”) {
document.getElementById(’email’).value = ‘[email protected]’;
}
document.getElementById(’email’).onfocus = emailFieldFocus;
document.getElementById(’email’).onblur = emailFieldBlur;

if (document.getElementById(‘author’).value == ”) {
document.getElementById(‘author’).value = ‘Given and Family name’;
}
document.getElementById(‘author’).onfocus = authorFieldFocus;
document.getElementById(‘author’).onblur = authorFieldBlur;

if (document.getElementById(‘url’).value == ”) {
document.getElementById(‘url’).value = ‘http://example.net/’;
}
document.getElementById(‘url’).onfocus = urlFieldFocus;
document.getElementById(‘url’).onblur = urlFieldBlur;

document.getElementById(‘searchForm’).onsubmit = searchFormRemoveExamples;
document.getElementById(‘newsletterForm’).onsubmit = newsletterFormRemoveExamples;
document.getElementById(‘commentForm’).onsubmit = commentFormRemoveExamples;
}[/code]

From [url=http://opensource-notebook.com/lib/script/formBehavior.js]http://opensource-notebook.com/lib/script/formBehavior.js.[/url] Used on pages such as [url=http://opensource-notebook.com/subscribe/]this one[/url] (where not all elements in the script are pressent).

Round-up: If not all of these ellements: s, author, email, url, searchForm, newsletterForm and commentForm are pressent on the page, the script fails.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@yellabuffDec 15.2006 — Add...

if ([COLOR=Red]document.getElementById('s') && [/COLOR] document.getElementById('s').value == '') {

document.getElementById('s').value = 'Search...';

}
Copy linkTweet thisAlerts:
@MoldarinauthorDec 15.2006 — Thanks! ?
×

Success!

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