hidura
hidura

Reputation: 693

Problem with JS function on Opera

function getLast(tagNm) {
    /* This function search the last element
     * that use this tagName */
    var cont = 0;

    $.each(window.wdgList, function (index, value) {
        if (value != undefined) {
            if ($("#" + value).get(0).tagName == tagNm) {
                cont += 1;
            }
        }    
    });

    return cont;
}

And with all the browser-including IE- this function works perfect but with Opera doesn't, what could be wrong.

BTW, the error says this:

The mistake is this: Uncaught exception: TypeError: Cannot convert 'document.getElementById(value)' to object

Upvotes: 0

Views: 2372

Answers (1)

user113716
user113716

Reputation: 322492

Hard to say with the info provided, but if for some reason Opera isn't finding one of your elements, then .get(0) will be undefined, and you'll be attempting to access the tagName property on undefined which will result in a TypeError.

You should perhaps check that an element was found first.

$.each(window.wdgList, function (index, value) {
    if (value != undefined) {
        var el = $("#" + value).get(0);
        if ( el && el.tagName == tagNm ) {
            cont += 1;
        }
    }    
});

This makes sure there's an element before doing element.tagName.

Upvotes: 2

Related Questions