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" });
}
});
Try this way:
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
sprintfand you could do something like:$(".DropDownMenu span:eq(%parentIndex)"But that is just wishful thinking.