Useful Javascript Code Snippets

Stop a form from submitting on Submit button click

form.addEventListener("submit", (event) => {
    event.preventDefault();
});

Submitting a form on user action

const button = document.getElementById("regular-button");

button.addEventListener("click", (event) => {
    document.getElementById("userForm").submit();
});

Read CSV lines pasted in <textarea> one by one

In the HTML, it could look like so

<div>
    <textarea type="textarea" name="file" id="file" rows="5" cols="33"> </textarea><br><br>
    <input type="button" id="btnsubmit" value="Submit" onclick="readCSVFile();" >
</div>

This is how tje JS would read it

function readCSVFile() {

    var textArea = document.getElementById('file');
    var lines = textArea.value.split('\n');

    // Loop through each line
    for (var x = 0; x < lines.length; x++) {
        
        setTimeout(generatePDF(lines[j]), 1000);

    }
	
};
comments powered by Disqus