1

I have done this before, but for some reason my current implementation isn't working.

I have some checkboxes and when clicked, I want that value put into an array. When that checkbox is clicked again, I want to remove that value.

HTML:

<div class="fieldwrap form-filters">
  <label for="filter0" class="icon-text checkbox">
      <input type="checkbox" id="filter0" name="filter0" value="Coast" class="hidden" />
    Coast</label>
  <label for="filter1" class="icon-text checkbox">
      <input type="checkbox" id="filter1" name="filter1" value="Great views" class="hidden" />
    Great views</label>
  <label for="filter2" class="icon-text checkbox">
      <input type="checkbox" id="filter2" name="filter2" value="Historic" class="hidden" />
    Historic</label>
  <label for="filter3" class="icon-text checkbox">
      <input type="checkbox" id="filter3" name="filter3" value="Moorland" class="hidden" />
    Moorland</label>
  <label for="filter4" class="icon-text checkbox">
      <input type="checkbox" id="filter4" name="filter4" value="Wildlife" class="hidden" />
    Wildlife</label>
  <label for="filter5" class="icon-text checkbox">
      <input type="checkbox" id="filter5" name="filter5" value="Woodland" class="hidden" />
    Woodland</label>
</div>

JS:

var filter_options = [];

$('.form-filters input:checkbox').click(function() {

    var name = $(this).val().trim();
    var index = filter_options.indexOf(name);
    if (index > -1) {
        filter_options = filter_options.slice(index, 1);
        console.log('Remove: '+name+' at index: '+index);
    } else {
        filter_options.push(name);
        console.log('Add: '+name);
    }

    $('#result').html(filter_options.join('; '));
    console.log(filter_options);

});

DEMO: http://jsfiddle.net/WTzKR/873/

For some reason it seems to randomly remove the values, but seems to always add them.

What have I done wrong?

1
  • For remove, do you not need to do slice(0, index) and slice(index + 1) to get the array parts before and after the remove index? Commented May 26, 2015 at 0:50

4 Answers 4

1

Simply change:

filter_options = filter_options.slice(index, 1);

to

filter_options.splice(index, 1);

in your if block.

splice is what you are looking for.

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

1 Comment

Yes, that worked! Thank you, such a simple mistake, but it made all the difference. Appreciate your help.
0

Try this:

<script>
        var filter_options=[];
        $('.form-filters input:checkbox').click(function()
        {
            var name=$(this).val().trim();
            if(this.checked)
            {
                filter_options.push(name);
                console.log('Add: ' + name);
            }
            else
            {
                var index=filter_options.indexOf(name);
                if(index > -1)
                {
                    filter_options.splice(index, 1);
                    console.log('Remove: ' + name + ' at index: ' + index);
                }
            }
            $('#result').html(filter_options.join('; '));
            console.log(filter_options);
        });
    </script>

Comments

0

Try input:checkbox:checked selector to get only checked ones:

var filter_options = [];

$('.form-filters input:checkbox').click(function() {
    filter_options = $('.form-filters input:checkbox:checked').map(function(){
      return this.value; //get the checkbox value
    }).get();//get all values as array
    $('#result').html(filter_options.join('; '));
    console.log(filter_options);
});

Comments

0

var filter_options = [];

$('.form-filters input:checkbox').click(function() {
	filter_options = [];
	$('.form-filters input:checkbox').each(function(idx, obj) {
        if($(obj).is(":checked"))
            filter_options.push($(obj).val());
    });
	$('#result').html(filter_options.join('; '));
	console.log(filter_options);
	
});

Comments

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.