Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problems with Array.prototype.indexOf.call

Tags:

javascript

If i click the a element "Link1" e.target in the function is the node Link1. I want to know in what index this node is in the ul children in this case i want indexOf to return 0 because Link1 is on position 0, and i i click 2 i want it to be 1.

HTML

<div class="link">
    <ul>
       <li><a>Link1</a></li>
       <li><a>Link2</a></li>
    </ul>
</div>

JAVASCRIPT

self.query('.link').forEach(function(linkNode, flikIndex, flikArr) { 
    dojo.query(linkNode, 'click', function(e) {
        var t = e.target; //If i click Link1 this is Link1 and if i click Link2 and so on. 
        var parent = t.parentNode; //Contains the parent to my a in this case li 
        var ancestor = t.parentNode.parentNode.childNodes; //Containes 2 li 
        var index = Array.prototype.indexOf.call(parent, ancestor); //Return -1 but i want it to return 0 because Link1 is on place [0] in the array.             

    }
}

Please help me get the right index

like image 470
rubin Avatar asked Nov 20 '25 10:11

rubin


1 Answers

var index = Array.prototype.indexOf.call(parent, ancestor);
//                                       ^ Array ^ Element in the array

So you want this:

var index = Array.prototype.indexOf.call(ancestor, parent);

Since ancestor is the element ("array") that contains parent.

like image 113
James Allardice Avatar answered Nov 22 '25 23:11

James Allardice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!