I am using script type module to execute some function which is written in addblogger.js.
This is my html code:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AddBlogging</title>
<!-- Custom styles for this template -->
<link href="AddBloggsstylesheet.css" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
<script type="module" src="./js/blogger.js " async></script>
<script type="module" src="./js/addblogging.js " async></script>
</head>
<body>
<hr />
<div class="container ">
<form class="form-group " action="AddBloggs.html " onsubmit="valid()">
<h2>Blogger Page</h2>
<label>Title</label>
<input type="text " name="Title " id="title " class="form-control " /><br />
<label>TextArea</label>
<textarea name="comment " id="textarea1 " placeholder="Text " class=" form-control "></textarea>
<br />
<label>Image</label>
<input type="file" name="myImage" id="myimage" accept="image/png, image/gif, image/jpeg" class="form-control " />
<br />
<br />
<br />
<!-- <input type="submit " value="submit " class="btn btn-success " /> -->
<input type="submit" value="submit" class="btn btn-success " />
<input type="reset" value="reset" class="btn btn-info " />
</form>
</div>
<hr />
<div class="container ">
<div class="row row-cols-auto ">
<div class="col ">
<div class="rightcolumn ">
<h2>TITLE HEADING</h2>
<h5>Title description, Sep 2, 2017</h5>
<!-- <div class="fakeimg " style="height:200px; ">Image</div> -->
<p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
</div>
</div>
</div>
</div>
</body>
</html>
Now I create a class blogger type. Hare blogger.js code:-
class blogger {
constructor(
title,
textarea,
image
) {
this.title = title;
this.textarea = textarea;
this.image = image;
}
}
export default blogger;
Now I want to create a list of blogger object using submit button in above html code:-
Example:-addblogging.js
import blogger from "./blogger.js";
let listofobjects = [];
const validate =
function() {
alert("Welcome to External JS file")
let title = document.getElementById("title").value;
let textarea = document.querySelector("#textarea1").value;
let image = document.getElementById("myimage").value;
var obj = new blogger(
title,
textarea,
image
);
listofobjects.push(obj);
document.write(listofobjects);
}
But somehow validate code not executed.I call addblogger.js using (type = module).When I remove import statement from addblogger.js and bypass other code apart from alert staement...I got alert messege.
Could you please help in this regards,why I am unable to execute valid() funtion.
Here is the picture valid funtion seems like it is not called.