Javascript Error: Null or not an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sinister747
    New Member
    • Jul 2008
    • 13

    Javascript Error: Null or not an object

    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.

    Code:
    function attributesimages(field_id) 
    {	
    	if (document.forms['addform'].image_caption_0.value == "") {
    		// Forget it...
    	} 
    	else
    	{
    		document.forms['addform'].field_id.options[1] = new Option('1.' + document.forms['addform'].image_caption_0.value,'0');
    	}
    
    }
    so the thought is that field_id would be provided to the script by onchange="retur n attributesimage s(this.id);" however when i run the script the below error comes back;

    Code:
    Line: 252
    Char: 3
    Error: 'document.forms.addform.field_id.options' is null or not an object
    Code: 0
    All help appreciated.

    Regards,
    Justin
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to Bytes!

    If you're passing the ID then document.getEle mentById(field_ id) is what you need.

    Comment

    • Sinister747
      New Member
      • Jul 2008
      • 13

      #3
      Originally posted by acoder
      Welcome to Bytes!

      If you're passing the ID then document.getEle mentById(field_ id) is what you need.
      Worked a charm, thank you very much

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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]...

        Comment

        Working...