Search code examples
javascriptprototypejs

Object doesn't support property or method 'previous' in Internet Explorer


I have a script code using Prototype.js that produces an error:

$('up-qty-item').observe('click',function(event){
    var currentElement = $('up-qty-item').parentNode.previous();
    var i = 0; //In case we get in to infinite loop
    while(currentElement.type != 'text' && i < 5){
        currentElement = currentElement.previous();
        i++;
    }
    currentElement.value = parseInt(currentElement.value) + 1;
    if ($('down-qty-item').hasClassName('disabled')){
        $('down-qty-item').removeClassName('disabled');
    }
});

When I click on the following button here:

<button type="button" id="up-qty-item" title="<?php echo $this->__('Add Item') ?>" class="button btn-qty up-qty-item">
    <span>+</span>
</button>  

I get the following error in the Internet explorer console. It works correctly in other browsers.

SCRIPT438: Object doesn't support property or method 'previous'

How do I eliminate this error?


Solution

  • I was able to eliminate the error using following way:

    ...
    var currentElement = $($('up-qty-item').parentNode).previous();
    ...
    

    and the error is gone.

    Reference url : http://prototypejs.org/learn/extensions

    // this will error out in IE: 
    $('someElement').parentNode.hide();
    // to make it cross-browser:
    $($('someElement').parentNode).hide();