Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define 2 classes in one var?

I have a problem with define a second class in one Var

Here is the Code: http://jsfiddle.net/2DuQc/

How can I make work this fine?

It's written in JQuery!

var animateEye = $('.rightEye, .leftEye'); 

doesnt work!

like image 383
user3714925 Avatar asked Mar 20 '23 09:03

user3714925


2 Answers

Actually your code won't work because you are overwriting your variabile with only one selector .leftEye so the .rightEye will not have nothing attached to it.

You can use a multiple selector than loop through the result using jQuery each and apply yuor current code.

Use the same class as inner element and find it as a children of the current element.

Code:

$('.rightEye, .leftEye').each(function () {
    var animateEye = $(this);
    var eyes = animateEye.find(".Eye");

Demo: http://jsfiddle.net/IrvinDominin/rU9E2/

like image 169
Irvin Dominin Avatar answered Mar 28 '23 19:03

Irvin Dominin


var animateEye = $('.rightEye, .leftEye');

This will give you an array of items, so when you call, say, animateEye.offset(), which one is it supposed to choose? It'll bomb out.

Your current code:

var animateEye = $('.rightEye');
var  eyes = $(".Eye");
var animateEye = $('.leftEye');

This will obviously always use .leftEye.

My suggestion? Loop through rightEye and leftEye like so:

var eyes = $('.rightEye, .leftEye');
for(var i = 0; i < eyes.length; i++) {
    var animateEye = eyes[i];
}
like image 20
Chris Dixon Avatar answered Mar 28 '23 20:03

Chris Dixon