Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.is(':even') does not work with a variable selector?

I am using jQuery to check if an element is odd or even:

 $("#map183").parent().is(':even');

This will return true or false depending on where the element is.

However this will always return false:

   function whereAreWe(myself,range){
        var parent = myself.parent();
        var position = parent.is(':even');
        alert(position);
        //outputs false
   }

   $('.hasSVG').on('click', function(){
        whereAreWe($(this),2);
   });

I have the working example here: JSFiddle

In the example you will see that i am trying to see if the parent(.line) of said element is either odd or even.

like image 926
Jamie Hutber Avatar asked Feb 05 '13 01:02

Jamie Hutber


1 Answers

This is because the :even selector is only useful within a list but parent refers to exactly one object. Therefore, :even with a list of 1 does not make sense.

However, you have IDs that indicate the line-number

var position = parent.attr("id").substr(4) % 2 == 0;
console.log(position);

extracting the id and deleting the "line"-string from "lineX" gives you the line-number X. A check with MOD 2 (or alternatively a bitwise AND with 1:)

var position = ((parent.attr("id").substr(4) & 1) == 1);

will do the trick.

like image 137
El Hocko Avatar answered Sep 30 '22 19:09

El Hocko