Slab
Slab

Reputation: 363

Jquery - Making a var equal $(this)

OK I apologise for the basic nature of this question but I'm taking baby steps here.

Why when I run the following, does maxOdd = 0 at the end of it all? I can see the width values coming out in the conside, but the if loop is not ever triggering.

var maxOdd = 0;
var maxEven = 0;

console.log("First level menu (6) odd num widths");
$('.menu-header ul li:nth-child(6) ul li:nth-child(odd)').each(function() {
    var $this = $(this);
    if ($this > maxOdd) {
        maxOdd = $this;
        console.log("this was bigger than maxOdd");   
    }
console.log($this.width());   
});

console.log("Max Odd = " + maxOdd);

Upvotes: 0

Views: 53

Answers (1)

Sagar Rabadiya
Sagar Rabadiya

Reputation: 4321

seems you are comparing wrong thing in if you are comparing instance instead of its width so there you go

if ($this.width() > maxOdd) {
        maxOdd = $this.width();
        console.log("this was bigger than maxOdd");   
    }

Upvotes: 4

Related Questions