473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using JavaScript and determining whether an element exists

Is there any way to determine whether an element in a document exists.

I have some code like this where the variable i changes:

if(document.getElementById('pref'+i).innerHTML...

When it tries to check an element that doesn't exist I get this error:

document.getElementById("pref" + i) has no properties

Is there anything I can do check whether an element exists before trying to use it or is there an exception I can catch to prevent the error from sdisruping the code.

Any help would be much appreciated.
Cheers,
Jonny
Feb 2 '07 #1
4 10029
acoder
16,027 Expert Mod 8TB
Welcome to The Scripts.

Yes, you can use:
Expand|Select|Wrap|Line Numbers
  1. if (document.getElementById("pref" + i))
to test for the object/element.
Feb 2 '07 #2
Thanks very much!
Jonny
Feb 2 '07 #3
acoder
16,027 Expert Mod 8TB
No problem, you're welcome.
Feb 2 '07 #4
dmjpro
2,476 2GB
he is right ....
but u can also use if(typeof document.getElementById('id_name') == 'undefined')
//the element does not exist..
thankkkssss...............
Feb 3 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Roy Wang | last post by:
hi, My problem is how to determining when the XML file has loaded using javascript. I loaded an xml file using javascript in a web page. The code below is loading the xml file for IE:...
12
by: daniel kaplan | last post by:
Hi All, Been learning Javascript (via google) to create forms with pre-set values. As seen below. My problem I have found is this: I can't seem to figure out (or find so far via google) how to...
3
by: Weston C | last post by:
I'm coding up a small little script that's supposed to be used to display the number of hours until the US Financial markets open/close. Naturally, this involves getting the current time on the...
3
by: Gareth Tonnessen | last post by:
I need to have a clean way to determine whether a query exists without trying to open it and getting an error message. Is there a simple way to determine whether an object exists in a database?...
14
by: Kayle | last post by:
How should we check if the '\0' characters exists in the string as I am confused that some books mentioned that we have to check whether we need to make sure that we pass the...
0
by: rwoo_98 | last post by:
I am using a repeater, with 3 columns of textboxes with X number of rows. The first two columns are enabled and allows the user to enter in numeric data. The third column contains the sum of the...
3
by: dlawner | last post by:
Anyone know a way to determine if a particular url is a trusted site using javascript? I need to validate that a site is trusted before the user logs on.
4
by: Nathan Sokalski | last post by:
When determining whether a String can be converted to a DateTime, you can use the IsDate() method. However, I would also like to know whether the string is a date, a time, or both a date and a...
1
by: vivek kapile | last post by:
Title:Dynamically adding table row with a checkbox using JavaScript Author:Vivek Kapile Email:snipped Language:JavaScript Platform:JavaScript in ASP.net Technology:Used in ASP.net...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.