0

I have the following jQuery code:

Working code

if(report==='Group 1'){
    $('.spandiv').not(':eq(17)').remove();
}

This works fine and removes all checkboxes that don't equal 17. However what I am trying to do now is only remove certain checkboxes.

I have tried the following:

Non-working code

if(report==='Group 1'){
    $('.spandiv').is(':eq(17)').remove();
    $('.spandiv').is(':eq(16)').remove();
    $('.spandiv').is(':eq(12)').remove();
    $('.spandiv').is(':eq(6)').remove();
    $('.spandiv').is(':eq(5)').remove();
    $('.spandiv').is(':eq(4)').remove();
}

I had assumed (clearly incorrectly) that this would work but it doesn't. I am clearly doing something wrong and would appreciate any feedback/ assistance.

2
  • 1
    What about just $('.spandiv:eq(17)').remove();? Commented Jul 2, 2014 at 16:25
  • Oopss...clearly not a good day for me. Thanks @j08691. Commented Jul 2, 2014 at 16:27

2 Answers 2

1

.is method will return boolean true or false if the selector matches the selected element. You are looking for .eq method or :eq selector (or .filter in the last case):

$('.spandiv:eq(17)').remove();  // this is the same as
$('.spandiv').eq(17).remove();  // this and the same as
$('.spandiv').filter(':eq(17)').remove();  // this
Sign up to request clarification or add additional context in comments.

Comments

1
$('.spandiv:eq(17)').remove();
$('.spandiv:eq(16)').remove();
$('.spandiv:eq(12)').remove();
$('.spandiv:eq(6)').remove();
$('.spandiv:eq(5)').remove();
$('.spandiv:eq(4)').remove();

The is function is just a boolean check. You could replace it with .filter() or .eq() too:

$('.spandiv').eq(17).remove();
$('.spandiv').eq(16).remove();
$('.spandiv').eq(12).remove();
$('.spandiv').eq(6).remove();
$('.spandiv').eq(5).remove();
$('.spandiv').eq(4).remove();

1 Comment

Cheers for that, appreciated and for the further feedback.

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.