This is my first time using javascript classes . I am using classes to return an html form . When the form is submitted I want to show an alert box that says " "submitted" . I have a problem doing this with classes as my submit function is not recognised .
export default class Contact extends AbstractView {
constructor(){
super();
this.setTitle("Contact Us");
}
test(e){
e.preventDefault();
alert('submitted');
}
async getHTML(){
return `
<form method = "POST" onsubmit = "test(this)">
<h2 > Send Message </h2>
<div class = "inputBox">
<input type = "text" name = "" required = "required" />
<span> Full Name </span>
</div>
<div class = "inputBox">
<input type = "submit" name = "" value="Send"/>
</div>
</form>`;
}
}
I want this test() function specifically for my contact object so I did not initialize it in my constructor .
The form is submitted with no alert shown and I get no error .
I am using classes to return html code for a single page app using vanilla js .
My index.js handles this functionality based on the url hash and appends the html from getHtml()
to a <div>
index.js
import DashBoard from './views/DashBoard.js';
import Posts from './views/Posts.js';
import Settings from './views/Settings.js';
import Contact from './views/Contact.js';
window.addEventListener("hashchange",()=>Router() );
document.addEventListener("DOMContentLoaded" , ()=>{
document.body.addEventListener('click',(e)=>{
if(e.target.matches("[data-link]")){
e.preventDefault();
navigateTo(e.target.href);
}
});
Router();
});
function navigateTo(url){
history.pushState(null,null,url);
Router();
}
async function Router (){
const routes = [
{path: "#" , view:DashBoard},
{path: "#posts" , view:Posts},
{path: "#settings" , view:Settings},
{path:"#contact", view:Contact},
];
const potentialMatches = routes.map((route)=>{
return {route:route , isMatch:location.hash===route.path}
});
let match = potentialMatches.find(potentialMatch=>potentialMatch.isMatch);
if(!match){
match = {
route:routes[0],
isMatch:true,
}
}
const view = new match.route.view();
let content = await view.getHtml();
document.querySelector('#app').innerHTML = content;
};