2

In my Script, I have an jQuery onmouseover() bound to list items with a div as parent. Now when I move the mouse over the li items, I want to get the index of this item in relation to its parent element (div).

so i use

var index = $($(this).index());

to get the hovered elements index as integer. The jquery documentation says

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

but the index var is an object. If I try to alert it (instead of using console.log for basic debugging), the dialogs' content is "[object object]".

Can you help me? Why is my code block returning an object although the documentation says it would return an integer when called without arguments?

2
  • 2
    Why did you wrap the code into another $(...)? Commented Jul 16, 2012 at 0:34
  • As @AurelioDeRosa suggests, just use var index = $(this).index() Commented Jul 16, 2012 at 0:35

1 Answer 1

5

All you have to do is get rid of the additional $(...). The code should be this:

var index = $(this).index();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. When I review my Code block I simply don't understand why I wrapped it in an additional object block…

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.