By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

Javascript Error: Null or not an object

P: 13
Hi All,

I have been spending all night and a large portion of today trying to fix this error, and this is all because i have limited knowledge of javascript.

This is my first post on IT Bytes but i have found most of my answers here before; so i am hoping you's can help me.

I have a javascript that when an onchange event on a select field is triggered it checks the page for previously entered image locations and then enters these image locations into the previously mentioned select box.

This script works fine if i hardcode the field id into it, however i wish to dynamically pass the field id into the script and have it accomplish the task (which is wont). Anyway enough yapping from me the code is below thank you.

Expand|Select|Wrap|Line Numbers
  1. function attributesimages(field_id) 
  2. {    
  3.     if (document.forms['addform'].image_caption_0.value == "") {
  4.         // Forget it...
  5.     } 
  6.     else
  7.     {
  8.         document.forms['addform'].field_id.options[1] = new Option('1.' + document.forms['addform'].image_caption_0.value,'0');
  9.     }
  11. }
so the thought is that field_id would be provided to the script by onchange="return attributesimages(;" however when i run the script the below error comes back;

Expand|Select|Wrap|Line Numbers
  1. Line: 252
  2. Char: 3
  3. Error: 'document.forms.addform.field_id.options' is null or not an object
  4. Code: 0
All help appreciated.

Jul 19 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 15k+
P: 16,027
Welcome to Bytes!

If you're passing the ID then document.getElementById(field_id) is what you need.
Jul 19 '08 #2

P: 13
Welcome to Bytes!

If you're passing the ID then document.getElementById(field_id) is what you need.
Worked a charm, thank you very much
Jul 19 '08 #3

Expert Mod 15k+
P: 16,027
You're welcome :)

Two other alternative ways are to pass 'this' to the function and then simply using field.options... The other method is to pass the name of the element and use document.forms["addform"].elements[field_name]...
Jul 20 '08 #4

Post your reply

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