I would like to understand what each component in the Javascript statement below means. I searched and found bits and pieces but nothing I could interpret for the whole set. The context is that I think this is the first statement that I should use to process my form.
Here is the first tag of my form element in my HTML document:
<form action="death.php" method="get" class="deathsEntry">
where “death.php” is the filename that I expect to find data passed to the backend php code, and the class="deathsEntry"
is a class variable name I gave to the form to identify it, and here is the javascript I need help with:
document.individual("deathsEntry").addEventListener("submit", function(event) {
event.preventDefault(); // Prevent form submission
As I understand it document
refers to the HTML DOM named document. .individual
I could find no explanation for in any of my books or any of the tutorials I took or by searching. Based on the color VS Code gives it, I think it is a qualifier for document
but I don’t know what it does. From the way it is positioned and colored in VS Code, I would infer that it has a specific effect qualifying some part of the DOM document
object (in the way that getElementById
causes a specific thing to happen). Does it restrict it to the current document
somehow or to the individual form named deathsEntry
in the HTML?
.addEventListener("submit", function(event)
is a javascript function that is qualified to listen for a submit
button click that will execute the event
function, ```event.preventDefault();
I think the last function is there to keep the form from being submitted until the submit button is clicked, but I don’t understand it.
I’ve done tutorials and can now (mostly) recognize components of statements even if I don’t know how to suss out what they do. I found this code in a sort of monkey-see/monkey-do approach where I sought an example of form processing and tried to adapt it to my needs. Did I properly use the class="deathsEntry"
in the document.individual . . .
statement?
My interpretation of the javascript is:
Please comment on my explanation and correct all misunderstandings.