querySelectorAll('.export-btn')
: Selects all elements with the class export-btn
, i.e., the export buttons.closest('tr')
: This retrieves the closest table row (<tr>
) that contains the clicked button.querySelectorAll('td')
: Retrieves all the <td>
cells in the row.Array.from()
: Converts the list of cells into an array, which allows us to manipulate it easily.cells.pop()
: Removes the last cell (the one containing the export button), as we don’t want it in the CSV.textContent
: Extracts the text content from each cell.encodeURIComponent()
: Encodes the CSV string, making it safe for use in a URL.anchor.download
: Specifies the filename for the CSV file (in this case, row_data.csv
).anchor.click()
: Programmatically triggers a click on the anchor element, which starts the download.
Here’s a complete example of the HTML and JavaScript code together: