Forum Moderators: open

Message Too Old, No Replies

Checking for the existence of an element by id

document.getElementById("name") has no properties

         

coopster

10:29 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Need a push in the right direction here. I have an external javascript file that does some form editing. The form is dynamically generated based on the user accessing it, so certain elements are not going to be on the page. However, in the javascript file the edit is still there and the javascript will fail with the following message ...

Error: document.getElementById("name") has no properties
Source File: myfile.htm Line: 81

I know I can condition the javascript if it was in the same document as the script, but as I am using external javascript files this is not a decent workaround. I know I should be able to check for the element prior to the edit check, but cannot get it to work. What is the proper or preferred method?

Pixelgamer

10:32 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



I was thinking, if it was possible fr you, that when the site is created, to create all forms, but use some sort of dummy-forms with the id the j-scipt is searching for?

kaled

10:57 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps I'm misunderstanding the problem but......

var tmp = document.getElementById(idName);
if (tmp) alert('yippeee'); else alert('dohhh')

Kaled.

coopster

11:27 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, you were right on the money kaled. Like an idiot I had the ".value" property on the end and was overlooking it. Duh.
var name = document.getElementById('name').value

Thank you for pointing out my obvious mistake.