Prakhar
Prakhar

Reputation: 3536

jQuery pseudo classes

Is this correct?

var deleteIndex = 3;
$("ol li:nth-child(deleteIndex)").remove();

For some reason, this doesn't seem to work. Executing this clears the whole list.

Upvotes: 0

Views: 228

Answers (3)

justkt
justkt

Reputation: 14766

You need to use:

var deleteIndex = 3;
$("ol li:nth-child(" + deleteIndex + ")").remove();

so that deleteIndex is converted to 3. Or if 3 is a constant used only here, you could just use 3.

It really works.

Upvotes: 0

a'r
a'r

Reputation: 36999

You are adding the literal text 'deleteIndex' into the jQuery selector rather than the number contained in the variable. Try this instead:

var deleteIndex = 3;
$("ol li:nth-child(" + deleteIndex + ")").remove();

Upvotes: 8

Zakaria
Zakaria

Reputation: 15070

May be if you try this: var deleteIndex = 3; $("ol li:eq(deleteIndex)").remove();

Upvotes: 0

Related Questions