nth-type-of inverse

nth-type-of inverse

Hi. I have written the following function to return the index number of an element among its own types within its parent element, i.e. inverse of the selector function nth-type-of() ( noti stands for nth-type-of-inverse()). Now because jQuery functions like parent() or children() turns the selected element into a jQuery object so that other methods can be chained to it, it isn't possible to use native Javascript utilities like nodeName or parentNode. Is there a way to write this code in just jQuery, without using native Javascript methods? And also, can this code be improved? The function requires one argument, the element under action(clicked or hovered upon), and this should be passed as event.target, and not plain this, for the latter would send the function not the element under action, but its parent, i.e. if there is a p inside a div, and that p is clicked, using event.target would send the function p, while using this would send div.

  1. function noti(element) {
    var c=0;
    $(element).addClass('fttb'); //fttb stands for "for the time being". It
    //is a temporary and imaginary class.
    var p=element.parentNode.getElementsByTagName("*");
    for (var i=0;i<p.length;++i) {
    if (p[i].nodeName != element.nodeName)
    continue; //Checking for elements of the type of that of 'element'.
    if (p[i].parentNode != element.parentNode)
    continue; //Because p[i] consists of all descendants of the parent of
                   // element, and they may themselves contain elements of the type of that of element,     //which we don't want to count.
    if (!($(p[i]).hasClass('fttb')))
    ++c;
    else
    break; //when we get to the element, we exit, else go to next element.
    $(element).removeClass('fttb');
    return c+1;
    }

















I tried without adding and removing the imaginary class for comparison by comparing like this:

  1. if (p[i]==element)

but I got bizarre results, which suggest that I can't compare two elements in this way. Though I really think I have done exactly the same thing in line 09. However that seems to work okay, for all results have been correct so far with that, but not with this line of code instead of the class inclusion-exclusion. Can anybody tell me why?