Search code examples
javascriptprototypejs

Exception selecting buttons with prototype


My html header have:

<meta http-equiv="X-UA-Compatible" content="IE=7" >

In javascript I use:

showButtons: function() {
    $$("#score-window .buttons")[0].show();
},

In all browsers excluding IE9,10 it works perfect. In IE9,10 first call of function is OK, but next fail because $$ returns "undefined" and calling show() broke code.

update prototype 1.6.0.2 included in project.


Solution

  • There are 2 fixes to this - First is the direct fix

    $$('#score-window .buttons').invoke('show');
    

    This will iterate over the all of the elements that match the CSS selector and run show() on them

    only use this if you only have one element with class .buttons within #score-window

    this will work and not throw errors as if there are no elements that match the selector it will not fire invoke()

    OR

    Second fix I would check to see if the element exists and then show it

    if($$('#score-window .buttons').length > 0) {
        $$("#score-window .buttons")[0].show();
    }
    

    These fixes will directly fix the error - however I think you have a different core problem that you need to dig into to find out why the CSS selector is not finding an element the second time. Also if you are able to upgrade to PrototypeJS 1.7.1.