473,394 Members | 1,971 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.

Javascript - check object defined

200 100+
Good day,

I thought i had this sorted but it does not seem to check if the object is undefined / not created.

I'm trying to check if object is undefined & i tried a few methos but no luck
with the "value;" it returns the alert no to say that it is define where it is not

Please help

Expand|Select|Wrap|Line Numbers
  1.       var a = document.getElementById('VE10110INTEFT').value;
  2.       if (a === undefined)
  3.       { alert("yes");} else { alert("no");}
  4.  
  5. var a = document.getElementById('VE10110INTEFT').value;
  6. if (typeof(a) === undefined)
  7.  
  8. var a = document.getElementById('VE10110INTEFT');
  9. if (typeof(a) == "undefined")
  10.  
  11. var a = document.getElementById('VE10110INTEFT');
  12. if (a == "undefined")
  13.  
Jan 28 '10 #1
7 3614
ismailc
200 100+
I got it going :)

Expand|Select|Wrap|Line Numbers
  1.       var a = document.getElementById('VE10110INTEFT');
  2.  
  3.       if (a != null)
  4.       { 
  5.         alert("yes");
  6.       }
  7.       else
  8.       {
  9.         alert("no");
  10.       }
  11.  
Jan 28 '10 #2
Dormilich
8,658 Expert Mod 8TB
if an input is not filled, it returns "" (empty string).
Jan 28 '10 #3
ismailc
200 100+
Hi,
Thanks my problem was that the object was not created or loaded due to some business rules on the page & therefore i would get error on if condition to check value

Regards
Jan 28 '10 #4
Dormilich
8,658 Expert Mod 8TB
then do
Expand|Select|Wrap|Line Numbers
  1. var a = document.getElementById('VE10110INTEFT');
  2. if (a === undefined) { /* … */ }
Jan 28 '10 #5
ismailc
200 100+
Hi,

I got it going using "!= null"

I did try the below, but i would get the error on the first line when defining the var.

var a = document.getElementById('VE10110INTEFT');
if (a === undefined)

Would also not work.
if (document.getElementById('VE10110INTEFT') === undefined)

Thanks I'm okay on this topic :)
I appreciate your help & time
Jan 29 '10 #6
gits
5,390 Expert Mod 4TB
yes ... i think this is correct ... document.getElementById('nodeId') returns null when the element is not found ... so the check for null is sufficient.

kind regards
Jan 29 '10 #7
Dormilich
8,658 Expert Mod 8TB
yes ... i think this is correct ... document.getElementById('nodeId') returns null when the element is not found
just went to look it up (DOM level 2 Core) … it indeed returns null if the element doesn’t exist.

so you could also test (unless the DOM implementation is faulty)
Expand|Select|Wrap|Line Numbers
  1. var a = document.getElementById('VE10110INTEFT');
  2. if (a === null) { /* … */ }
Jan 29 '10 #8

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

Similar topics

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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.