1

I just want to dynamically put in the index, which is calculating correctly, so I know the value of my variable isn't a problem, but it's not working:

My variable 'parentIndex' stores the index of the span I want to be selecting below. I have tested this variable and it returns the correct value.

$(".DropDownMenu span:eq("+parentIndex+")")

is this not the right way to put a variable into a jquery selector? all the examples i've found use this format, what am i missing?

(The entire function, for context:)

        $("span input:radio").click(function() {
        if (($(this).is(":checked")) == true) {
            var elem = $(this);
            var parent = $(this).parent();
            var aunts = parent.parent().children();
            var parentIndex = aunts.index(parent);
            var position = elem.position();
            var topValue = position.top;
            topValue = topValue - 9;
            $(".DropDownMenu").css({ "top": "-" + topValue + "px" });
            $(".DropDownMenu span").css("background-image", "none");
            parent.css({ "background": "#f3f1e7 url(assets/images/branding/DropDownArrow.gif) no-repeat right" });
            $(".DropDownMenu span:eq("+parentIndex+")").css({ "background": "#f3f1e7 url(assets/images/branding/DropDownArrow.gif) no-repeat right" });

        }
    });

2 Answers 2

1

Try this way:

$(".DropDownMenu span").eq(parentIndex)

You can find more details of this in the docs here.

However, yours should work the way it is, make sure you use FireBug to test this, and maybe try it with a less complex function just to test it's functionality.


It would be cool if JS had something like sprintf and you could do something like: $(".DropDownMenu span:eq(%parentIndex)"

But that is just wishful thinking.

Sign up to request clarification or add additional context in comments.

1 Comment

it worked, it's not behaving the way i want, but that's my fault, my function is wrong
0

Have you debugged and seen what type parentIndex is?

try doing this:

$('.DropDownMenu span:eq('+ parseInt(parentIndex) +')');

1 Comment

yeah i did; it actually was working all along, I forgot i needed to use the index to figure out nth child. I was just being stupid. :-/

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.