1

I'm trying to get the index of the trigger element in the array.

The html looks like this:

<ul>
     <li><a href="#" rel="group">Link 1</a></li>
     <li><a href="#" rel="group">Link 2</a></li>
     <li><a href="#" rel="group">Link 3</a></li>
</ul>

Then on click I'm calling the run() method of the obJ object:

var obJ = {
    run : function(obj) {
        var att = obj.attr('rel');
        var arr = jQuery.find('a[rel='+att+']');
        alert(obj.indexOf(arr));
    }   
};
$(function() {
    $('a.click').click(function() {
        obJ.run($(this));
        return false;
    });
});

The above however doesn't give me the index of the trigger in the collected array.

Any thoughts?

3 Answers 3

4

You want jQuery.index().

var obj = {
    run: function(obj) {
        var att = obj.attr('rel');
        var index = $('a[rel='+att+']').index(obj);
    }   
};
Sign up to request clarification or add additional context in comments.

Comments

1

Don't you mean this?

alert(arr.indexOf(obj));

Comments

0

Mike Haboustak's answer is probably the best way of doing it but you could also do something like

$('a.click').each(function(index, o){
   $(o).click(function () { obj.run($(o), index); return false; });
});

Similar result slightly different approach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.