Search code examples
javascriptjasminegrunt-contrib-jasmine

Why does Jasmine complain about 'Attempted to assign to readonly property' when I set selectedIndex?


When I run this Jasmine spec through grunt-contrib-jasmine, I get the error TypeError: Attempted to assign to readonly property. pointing to the line where I set the selectedIndex attribute:

it('does something', function () {
    setFixtures('<select id="selectId"><option>Text</option></select>');
    var selectDOM = document.getElementById('selectId');
    selectDOM.selectedIndex = 0;
    // stripped some code here...
});

Does anybody know what I am missing here? I use phantomjs v1.9.8, grunt-cli v0.1.13 and grunt-contrib-jasmine v0.9.2.


Solution

  • Got it, I made an error, but since this can be hard to find I for now will leave my question and answer here. Feel free to delete it completely though.

    I made a typo in the value I passed into the getElementById() function, so the variable selectDOM was null. This for some reason led to the error "TypeError: Attempted to assign to readonly property.".

    The typo was hard to see for me and the error message misleading in this case, so others might run into the same issue.