Source: Pure JavaScript alternative to jQuery’s .remove()
Instruction initiale (en jQuery):
1 |
$stock.find('.is-cell:empty').remove(); |
Boucle for
:
1 2 3 4 5 6 7 |
// use querySelectorAll to return all matching DOM elements let $stockEmptyCell = $stock.querySelectorAll('.is-cell:empty'); for ( let i=0; i < $stockEmptyCell.length; i++ ) if ( $stockEmptyCell[i].innerHTML.length === 0 ) // use parentNode.removeChild to remove elements $stockEmptyCell[i].parentNode.removeChild( $stockEmptyCell[i] ); |
Sous forme de fonction
1 2 3 4 5 6 |
function removeStockEmptyCells( childrenElement, parentElement ) { let $childrenElement = parentElement.querySelectorAll(childrenElement); for ( let i = 0; i < $childrenElement.length; i++ ) if ( $childrenElement[i].innerHTML.length === 0 ) $childrenElement[i].parentNode.removeChild( $childrenElement[i] ); } |
1 |
removeStockEmptyCells( '.is-cell:empty', $stock ); |