JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 22nd, 2007, 12:10 PM
Damien-uk Damien-uk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 61 Damien-uk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 8 m 59 sec
Reputation Power: 9
How to check if ID exists in html page

Hi,

I'm doing some fairly basic client-side JS and wondered if there's a simple way to do something like:
Code:
if(getElementByID("someId"))
  //do something

At the moment I have some JS that I'm trying to re-use in a couple of places, but the IDs I'm operating on are different in each place.

I reckon there's probably a more elegant way that I could re-write the whole thing, but at this stage I don't have time for that...

I'm looking to avoid the JS errors that are occuring when my getElementByID() statements are executed on IDs not present on that particular page.

Any ideas?

Reply With Quote
  #2  
Old February 22nd, 2007, 12:11 PM
huyaroo's Avatar
huyaroo huyaroo is offline
:)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2006
Location: IL
Posts: 1,520 huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)huyaroo User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)  Folding Points: 303 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 6 Days 2 h 36 m 42 sec
Reputation Power: 438
Use try and catch:

http://www.w3schools.com/js/js_try_catch.asp

Code:

try
{
// do whatever you want with your getElementById here
}
catch(err)
{
// id does exist
}
__________________



shinygears | twinarrow

Last edited by huyaroo : February 22nd, 2007 at 12:14 PM.

Reply With Quote
  #3  
Old February 22nd, 2007, 12:28 PM
nev3rm0re's Avatar
nev3rm0re nev3rm0re is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: Estonia
Posts: 61 nev3rm0re User rank is Sergeant (500 - 2000 Reputation Level)nev3rm0re User rank is Sergeant (500 - 2000 Reputation Level)nev3rm0re User rank is Sergeant (500 - 2000 Reputation Level)nev3rm0re User rank is Sergeant (500 - 2000 Reputation Level)nev3rm0re User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 21 h 8 m 6 sec
Reputation Power: 15
Quote:
Originally Posted by Damien-uk
I'm doing some fairly basic client-side JS and wondered if there's a simple way to do something like:
Code:
if(getElementByID("someId"))
  //do something



The code you provided is the most elegant way of checking for element with specified ID.

That way you can see that code depends on a presence of a certain ID in the document, while try ... catch will catch all errors, even those that are not related to missing IDs.

I would recommend sticking with your code, it's elegant and good coding style.

Reply With Quote
  #4  
Old February 22nd, 2007, 02:25 PM
Damien-uk Damien-uk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 61 Damien-uk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 8 m 59 sec
Reputation Power: 9
Thanks to both of you - I wasn't aware of (or I think in fact had forgotten about) try / catch in JS!

That's a nice easy solution to that one then... cheers!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How to check if ID exists in html page


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap