0

I am trying to avoid using jQuery for DOM manipulation. I prefer to use ES6 features. ES6 has a robust array class, so I am considering converting HTMLCollection objects into arrays and then parsing them using the Array class:

actual = [ '10/12/2018', 'Affidavit', 'Party', 7 ];
collection = Array.prototype.slice.call( document.querySelector('#tblEvents').rows )
target_row = collection.find(function(row){
  predicate = row.cells[0].innerText.trim() == actual[0] && row.cells[1].innerText.trim() == actual[1] && row.cells[4].innerText.trim() == actual[3];
  if(predicate){
    if(actual[2] !== null && actual[2] !== ''){
      return actual[2] == row.cells[2].innerText.trim();
    } else {
      return true;
    } 
  }

  return false;
})

The es6 method I am using here is find(). But HTMLCollection has no such method.

Is this the appropriate solution for ES6 or does the HTMLCollection class offer something similar?

Daniel Viglione
  • 8,014
  • 9
  • 67
  • 101

3 Answers3

3

The easiest approach is to convert it to an array first with Array.from:

const collection = Array.from(document.querySelector('#tblEvents').rows);
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
0

If what you want is to simply manipulate the DOM you are OVER complicating things.

ES6 does not add anything new for DOM manipulation... As for jQuery it is simply a library that makes things easier for you (also normalizes different behavior between browsers).

DOM manipulation - searching, adding, deleting and editing elements from the page.

selecting a DOM element can be done using one of the following

  1. document.querySelector();
  2. doucment.querySelectorAll();
  3. document.getElementById();
  4. document.getElementsByClassName();
  5. document.getElementsByName();
  6. document.getElementsByTagName();

Editing:

let element = document.querySelector();
element.innerHTML = "Hello World";
element.setAttribute('data-custom', 123);

Deleting:

document.removeChild(childNode);        

You can read more at:

The HTML DOM Element Object

Document Object Properties and Methods

Stoil Ivanov
  • 104
  • 2
  • 3
0

Another way of converting a collection to an array is using the spread syntax:

const collection = [...document.querySelector('#tblEvents').rows];

There is this question about the most efficient way of concerting an HTMLCollection to an Array discussing the various ways: Most efficient way to convert an HTMLCollection to an Array

Felix B.
  • 166
  • 8