/    Sign up×
Community /Pin to ProfileBookmark

check id exists

Hi guys,

I’m trying the figure out how to check if a id exists.

I have tried this;

if(document.getElementById(“theID”)
Then do this

But that doesn’t seem to work.

All I want to do is if it doesn’t exist then don’t run the function.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JonaSep 22.2010 — [font=arial]Hi,

Convert it to a Boolean value. Here's the shortest way I can think of:[/font]

<i>
</i>if (!!document.getElementById('theID')){
// the element exists
} else {
// not found
}
Copy linkTweet thisAlerts:
@MrRedSep 22.2010 — I am sure I have done it (thanks to Dreamweaver functions) without converting to boolean per se.

see various pages on http://cresby.com for examples. the function that uses it is findObj{blah,blah}

Best of luck.
Copy linkTweet thisAlerts:
@JavaboeySep 22.2010 — if you used if(document.getElementById("theID"), which is copy/paste from your post, then you have a type-o. You left the last parenthesis off. Should be:

if (document.getElementById("theID"))

It will work just fine.
Copy linkTweet thisAlerts:
@tecmeisterauthorSep 22.2010 — if you used if(document.getElementById("theID"), which is copy/paste from your post, then you have a type-o. You left the last parenthesis off. Should be:

if (document.getElementById("theID"))

It will work just fine.[/QUOTE]


I didn't copy and paste it I just typed it quick as a example and it didn't work. I have tried what the others have said and it works
Copy linkTweet thisAlerts:
@JavaboeySep 22.2010 — Just FYI, it works. I tried it before I posted my first reply.
×

Success!

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