aoa
aoa

Reputation: 81

Comparing element ids in jquery

I'm trying to compare two element ids in jQuery with the following code.

if($("#pic" + i).attr("id") == ($this.attr("id")))

If I try to retrieve the ids individually I can, but when I try to compare them within the if statement it crashes my script, and I'm not sure why.

EDIT: The entire piece of code.

$(document).ready(function() {
    $(".slideshow").click(function() {
        $("#pic0").attr('class', 'a');
        $("#pic6").attr('class', 'a');


        $(this).css('z-index', 1);
        $(".slideshow").animate({
            left: '10px'
        }, 1000);

        for (var i = 1; i < 6; i++) {
            alert($("#pic" + i).attr("id"));
            alert($(this).attr("id"));

            if ($("#pic" + i).attr("id") == ($this.attr("id"))) {
                $("#pic" + i).removeClass("boxShadow");
                alert("Doesn't");
            }
        }

        $("#contenttable").show();
        $("#contenttable").animate({
            width: '1200'
        }, 1000);
        $("#fadecontent").fadeIn(4000);

        $("#pic0").attr('attr', "slideshow");
        $("#pic6").attr('attr', "slideshow");
    });
});

Upvotes: 0

Views: 11052

Answers (3)

rajesh kakawat
rajesh kakawat

Reputation: 10896

change your code from

this

 if ($("#pic" + i).attr("id") == ($this.attr("id")))

to

 var pic_id = "pic" + i;
 if (pic_id == (this.id))

Upvotes: 2

arulmr
arulmr

Reputation: 8836

Try using

$(this).attr("id")

instead of

$this.attr("id")

Upvotes: 2

Rooster
Rooster

Reputation: 10077

you are doing $this instead of $(this) inside your if statement....got darn php/jquery

Upvotes: 4

Related Questions