javascriptattributesbooleansetattribute

Set Custom attribute in Js


I am filtering a table and exporting it to excel. To exclude the filtered rows, i have to set the data-exclude="true" in the 'tr'-element

function Search() {
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[7];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "" ;
//Set here the Code data-exclude="false"
      } else {
        tr[i].style.display = "none";
//Set here the Code data-exclude="true";

      }
    }       
  }
}

``


Solution

  • You can update attributes for an element using setAttribute function

    tr.setAttribute('data-exclude', true)

    You can use getAttribute function to get the value of a specific attribute. Note that value is returned as a string and need to be converted to a boolean value in your case to filter