Convert HTML table tags to CSV using javascript

Recently, I was working in an old Coldfusion environment and I wanted to export a data from a database to CSV.

Instead of writing CSV parsing in Coldfusion I used this Javascript code.

function downloadCSV(csv, filename) {
    var csvFile;
    var downloadLink;

    // CSV file
    csvFile = new Blob([csv], {type: "text/csv"});

    // Download link
    downloadLink = document.createElement("a");

    // File name
    downloadLink.download = filename;

    // Create a link to the file
    downloadLink.href = window.URL.createObjectURL(csvFile);

    // Hide download link
    downloadLink.style.display = "none";

    // Add the link to DOM
    document.body.appendChild(downloadLink);

    // Click download link
    downloadLink.click();
}


HTML Code and Calling the above function
I displayed the data from table in a web page and called this function
I set text color as white.
setTimeout(function(){
exportTableToCSV('support_request.csv');
document.getElementById("table").style.visibility="hidden";
 }, 1000);

I agree. this is an indirect approach but this will be useful when your data is less than 1 MB and when working with old programming language or frameworks like Coldfusion, Adobe, COBOL 🙂

Reference: https://www.codexworld.com/export-html-table-data-to-csv-using-javascript/