How can I change this JavaScript code to a function?
Hi,
Could someone please help me to change this JavaScript code snippet to a function?
- <script>
- var headertext = [],
- headers = document.querySelectorAll("#responsiveTable th"),
- tablerows = document.querySelectorAll("#responsiveTable th"),
- tablebody = document.querySelector("#responsiveTable tbody");
-
- for(var i = 0; i < headers.length; i++) {
- var current = headers[i];
- headertext.push(current.textContent.replace(/\r?\n|\r/,""));
- }
- for (var i = 0, row; row = tablebody.rows[i]; i++) {
- for (var j = 0, col; col = row.cells[j]; j++) {
- col.setAttribute("data-th", headertext[j]);
- }
- }
- </script>