DirtyRedz
DirtyRedz

Reputation: 566

Object doesn't support property or method 'removeClass'

Im trying to do a simple removeClass and addClass to change styles on an Img.

    <div id="Pic_Viewer">
        <div id="Main_Pic_Viewer">
                <div class="Not_Selected" >
                    <img src='#' alt="PicURL_1" />
                </div>
                <div class="Not_Selected" >
                    <img src='#' alt="PicURL_2" />
                </div>
        </div>
        <div id="Small_Pic_Viewer">
            <ul>
                <li>
                    <img class="Small_Pic" src'#' alt="PicURL_1" />
                </li>
                <li>
                    <img class="Small_Pic" src='#' alt="PicURL_2" />
                </li>
            </ul>
        </div>
    </div>

I have tried doing this with the #Main_Pic_Viewer img inside div and without.

js:

$('#Small_Pic_Viewer ul li').click(
        function () {
            var ThisLI = this.firstElementChild.alt;
            var BigImgDiv = $('#Main_Pic_Viewer div');
            var CurDiv;

            for (var i = 0, l = BigImgDiv.length; i < l; i++) {
                CurDiv = BigImgDiv[i];
                if (BigImgDiv[i].children[0].alt === ThisLI) {
                    CurDiv.removeClass('Not_Selected').addClass('Selected');
                } else {
                    CurDiv.removeClass('Selected');
                };
            };
        }
    );

Not sure why im getting this error message, as removeClass() is working fine in other methods.

Upvotes: 3

Views: 4267

Answers (2)

Anujith
Anujith

Reputation: 9370

Try this: http://jsfiddle.net/Yx5c8/1/

$('#Small_Pic_Viewer ul li').click(function () {
    var ThisLI = this.firstElementChild.alt;
    $('#Main_Pic_Viewer div').each(function () {
        if (this.children[0].alt === ThisLI) {
            $(this).removeClass('Not_Selected').addClass('Selected');
        } else {
            $(this).removeClass('Selected');
        }
    });
});

Upvotes: 0

Sirko
Sirko

Reputation: 74076

When using a numeric index in a jQuery object you get the original DOM element(s) without the jQuery wrapper.

Just wrap this again in a jQuery function and you'll be fine:

// ...

CurDiv = $( BigImgDiv[i] );

// ...

Another solution as suggested by @Andreas in the comments is to use the eq() method, which is probably the better way:

// ...

CurDiv = BigImgDiv.eq(i);

// ...

Upvotes: 6

Related Questions