Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 15th, 2005, 11:03 PM
Authorized User
 
Join Date: Dec 2005
Location: , , .
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Default Using typeOf with getElementById()

I've had great success using the typeOf() to test for the existence of a field. I would use:

if( typeOf(document.MyForm.MyFormFieldName ) != 'undefined' )

However I guess CSS and XHTML is moving away from the Name attribute to the ID attribute. I'm not having any luck using typeOf with getElementByID(). I tried using the following:

if( typeOf(document.MyForm.getElementById('MyFormField ID') ) != 'undefined' )

What is the way to test for the existence of a form field using the ID attribute?

  #2 (permalink)  
Old December 16th, 2005, 03:38 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

getElementById is a method of the document, not the form, so you should use this:

document.getElementById('MyFormFieldID')

instead of this:

document.MyForm.getElementById('MyFormFieldID')

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #3 (permalink)  
Old December 16th, 2005, 08:19 AM
Authorized User
 
Join Date: Dec 2005
Location: , , .
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Default

Cool, thanks.



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using getElementById Frank Huddleston XML 4 November 11th, 2005 01:44 PM
Why getElementById("f2").style vinod_yadav1919 Javascript How-To 2 January 18th, 2005 11:00 AM
getElementByID error lian_a Javascript How-To 1 December 21st, 2004 01:42 AM
'getElementById()' anshul Javascript How-To 1 September 1st, 2004 03:57 PM
document.getElementById(). PKC XSLT 0 July 21st, 2004 02:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.