Carlos Farmer
Carlos Farmer

Reputation: 67

I keep getting 'unable to get property 'length' of undefined or null reference'

I'm testing independent pieces of a larger piece of code for functionality in MS CRM and I want to know why I keep getting the above error for this small piece of code:

function testThis()
{
    var optionset = document.getElementById("new_makeyear");
    console.log("this is supposed to be something " + optionset.Options.length);
}

"new_makeyear" is an option set. The log statement was just so I can see the behavior through the console. What's the problem?

Thanks

Upvotes: 0

Views: 848

Answers (2)

Polshgiant
Polshgiant

Reputation: 3664

As Henk mentioned in his comment, you shouldn't access the DOM. You should use the API to inspect the options of an option set, like this:

function testThis() {
    var makeYear = Xrm.Page.getAttribute("new_makeyear");
    if (!makeYear) { return; }
    console.log(makeYear.getOptions().length + " options currently available");
}

Upvotes: 2

Alex Kudryashev
Alex Kudryashev

Reputation: 9460

If you manage to access to generated DOM (for example via onchange event) then optiongroup is available as if(this.options[this.selectedIndex].parentNode.tagName =='optgroup') var og = this.options[this.selectedIndex].parentNode.label;

Upvotes: 0

Related Questions