3

On IE8 (not on IE9 or Safari) I get an error

this.text_array is null or not an object

for this line

`if( this.text_array[element].value === '' )` 

for this object -

/**
 *          JClass - Text
 */

var Text = function( form_name ) 
{
    this.text_array = document.forms[form_name].elements;
};

Text.prototype.patterns = 
{
    prefix_url:     /^(http:)|(https:)\/\//,
    url:            /^.{1,2048}$/,
    tweet:          /^.{1,40}$/, 
    title:          /^.{1,32}$/, 
    name:           /^.{1,64}$/, 
    email:          /^.{1,64}@.{1,255}$/,
    pass:           /^.{6,20}$/
};

Text.prototype.checkPattern = function( type ) 
{
    return this.patterns[type].exec( this.text_array[type].value );
};

Text.prototype.checkUrlAdd = function( type ) 
{
    return this.patterns[type].exec( this.text_array.url.value );
};  

Text.prototype.checkSameEmail = function() 
{
    return ( (this.text_array.email.value) === (this.text_array.email1.value) );
};

Text.prototype.checkEmpty = function() 
{
    var element;
    for ( element in this.text_array )
    {
        if( this.text_array[element].value === '' ) 
        {
            return 0;
        }
    }
    return 1;
};

Not sure where to begin troubleshooting this. I guess I could start by hard coding the element object...that would eliminate the DOM Pull as a suspect. I could continue in this way...but I don't have IE8 available right now. Trial and Error unless someone has a bit of insight.

Related SO

for in vs. for

Community
  • 1
  • 1
  • How are you calling `checkEmpty`? Keep in mind that `this` can bind to something other than the `Text` instance, depending on how you call it. Or maybe `document.forms[form_name].elements` really is `null` but I'm _sure_ you checked that ;) – Halcyon Feb 10 '12 at 21:49
  • checked by correlation...but that would be my first guess as well. –  Feb 10 '12 at 21:51
  • Note that `for..in` iterates properties, not just indexes. Use simple `for` loops to iterate Array indexes. Ref: http://stackoverflow.com/a/500531/15031 – Jonathan Lonowski Feb 10 '12 at 22:10

2 Answers2

1

Ended up using

document.getElementById()

for form access as a quick solution to fix the problem. Did not have time to troubleshoot further per suggestions.

  • To make this a proper answer, you should give the full details of the solution, i.e. show that you used document.getElementById instead of document.forms[form_name]. – Fenton Oct 17 '12 at 16:28
1

To start your debugging, use console.log(this.text_array); in relevant places.

I can only assume it's caused by this not being set right by the browser. In this case, start your checkEmpty function with var that = this; and use that instead of this through it.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592