javascriptjqueryjquery-selectorsattributesjquery-data

jQuery .data method - Dynamically adding or removing elements based on store data


I have created a search page using parametric filtering conducted on change of checkbox values.

http://jsfiddle.net/s9FjY/4/

    $('#parameters input[type="checkbox"]:enabled').bind('change', function(e) {
      var $this = $(this),
        $checkedBoxes = $('#parameters').find('input:checkbox').filter(':checked').length,
        index = $('#parameters input:checkbox').index($this),
        txt = jQuery.trim($this.parent().text());
      if ($checkedBoxes === 0) {
        //remove all filters
        $('#filters > ul > li').remove();
      } else {
        if ($this.is(':checked')) {
          // add filter text
          var filterLink = $('<li>' + txt + '<span>Remove filter</span></li>');
          $.data(filterLink, 'myIndex', index);
          alert($.data(filterLink, 'myIndex'));
          $('#filters > ul').append(filterLink);
        } else {
          // remove filter text
          $('#filters > ul').find('li[class=' + index + ']').remove();
        }
      }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<ul id="parameters">
  <li>
    <label for="premium">
      <input type="checkbox" id="premium" name="filters" value="true" />Premium
    </label>
  </li>
  <li>
    <label for="articles">
      <input type="checkbox" id="articles" name="articles" value="true" />Articles
    </label>
  </li>
  <li>
    <label for="news">
      <input type="checkbox" id="news" name="news" value="true" />News
    </label>
  </li>
  <li>
    <label for="other">
      <input type="checkbox" id="other" name="other" value="true" />Other
    </label>
  </li>
</ul>

<div id="filters">
  <p>Filters applied:</p>
  <ul>
    <!-- Filters applied insert here -->
  </ul>
</div>

I am displaying which filter was applied in a dynamic list and storing the association on the list filter element with jQuery's .data() method.

Now, when I un-check the appropriate checkbox I want to remove the filter li element based on the data that is stored on the said element. I was previously doing this on the class attribute but thought using .data() was a neater solution.

Can't quite figure out how to remove the appropriate list element?


Solution

    1. Why are you using class? You never defined class names.

    2. There is $(this).data which works seamlessly: http://jsfiddle.net/s9FjY/7/.

    3. You do not need separate code for 0 checked checkboxes.

    The code:

    $('#parameters input[type="checkbox"]:enabled').bind('change', function(e){
        var $this = $(this),
            $checkedBoxes = $('#parameters').find('input:checkbox').filter(':checked').length,
            index = $('#parameters input:checkbox').index($this),
            txt = jQuery.trim($this.parent().text());
        if ($this.is(':checked')) {
            // add filter text
            var filterLink = $('<li>' + txt + '<span>Remove filter</span></li>');
            $(filterLink).data('myIndex', index);
            alert($(filterLink).data('myIndex'));                
            $('#filters > ul').append(filterLink);
        } else {
            // remove filter text
            $('#filters > ul').find('li').filter(function() {
                return $.data(this, 'myIndex') === index;
            }).remove();
        }  
    });