.is (': even') not working with variable selector?

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

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

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

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 a working example here: JSFiddle

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

+6
source share
1 answer

This is because: even the selector is useful only in the list, but the parent object refers to only one object. Therefore: even with list 1 it makes no sense.

However, you have identifiers indicating the line number

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

extracting the identifier and deleting the line "line" from the line "lineX" gives you the line number X. Check with MOD 2 (or alternatively bitwise And with 1 :)

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

will do the trick.

+2
source

All Articles