javascript - Toggle(Hide/Show) <tr> using jQuery on specific attibute value -


i want hide <tr> based on value of attribute named status_id. , i'm using jquery datatable.

this have tried, not working.

this table, which, want toggle <tr> attribute status_id in <span>

<button id="hide-tasks" class="hide-tasks" type="button">toggle</button> <table id='tbl_tasklist'> <tbody>     <tr role="row" class="odd">         <td style="padding-right: 5px;">             <span data-title="new - unclaimed / unassigned" class="data-hover stat_195 clas clicktip  badge  stat-adjust btn-list fa fa-adjust" status_id="4" move-left="500">&nbsp;</span>         </td>     </tr> </tbody> 

and js that,

$(document).on('click', '.hide-tasks', function (e) {     var toggleval = [1, 9, 10, 11, 12];     var statustoggle = $(this).closest('tr').children('span').find('status_id');     if (statustoggle === toggleval) {         statustoggle.toggle();     }  }); 

where wrong?

firstly status_id attribute of span, use of find() incorrect.

secondly, button not within table, closest() not find goes dom. need combination of next() , find(). need loop through span element found in each tr using each()

finally note creating own non-standard attributes mean html invalid. store custom data element use data-* attributes.

from there can use indexof() discover if status-id in array, , toggle() required element needed. try this:

var togglevalues = [1, 9, 10, 11, 12];    $(document).on('click', '.hide-tasks', function(e) {    $(this).next('table').find('span').each(function() {      var $el = $(this);      $el.closest('tr').toggle(togglevalues.indexof($el.data('status-id')) == -1);    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <button id="hide-tasks" class="hide-tasks" type="button">toggle</button>  <table id='tbl_tasklist'>    <tbody>      <tr role="row" class="odd">        <td style="padding-right: 5px;">          <span data-title="new - unclaimed / unassigned" class="data-hover stat_195 clas clicktip  badge  stat-adjust btn-list fa fa-adjust" data-status-id="1" move-left="500">1</span>        </td>      </tr>      <tr role="row" class="odd">        <td style="padding-right: 5px;">          <span data-title="new - unclaimed / unassigned" class="data-hover stat_195 clas clicktip  badge  stat-adjust btn-list fa fa-adjust" data-status-id="2" move-left="500">2</span>        </td>      </tr>      <tr role="row" class="odd">        <td style="padding-right: 5px;">          <span data-title="new - unclaimed / unassigned" class="data-hover stat_195 clas clicktip  badge  stat-adjust btn-list fa fa-adjust" data-status-id="9" move-left="500">9</span>        </td>      </tr>      <tr role="row" class="odd">        <td style="padding-right: 5px;">          <span data-title="new - unclaimed / unassigned" class="data-hover stat_195 clas clicktip  badge  stat-adjust btn-list fa fa-adjust" data-status-id="10" move-left="500">10</span>        </td>      </tr>      <tr role="row" class="odd">        <td style="padding-right: 5px;">          <span data-title="new - unclaimed / unassigned" class="data-hover stat_195 clas clicktip  badge  stat-adjust btn-list fa fa-adjust" data-status-id="15" move-left="500">15</span>        </td>      </tr>    </tbody>


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -