I try to make a button on my webpage once i press it, it will turn up a div box with a google map. But it doesn't work ! it turns up with javascript syntax Error"Uncaught TypeError: Cannot read property 'offsetWidth' of null" please helps!
the chrome development tool, turning up with javascript syntax problems. This is my first time using Google map API to my webpage.
<! DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Google book search </title>
<!--link rel="stylesheet" href="css/style.css"-->
</head>
<body>
<div id = "input">
<form>
<input type="button" id="search_button" value="search">
</form>
</div>
</body>
</html>
<script >
var body = document.getElementsByTagName("body")[0];
var dosearch= function() {
var newDiv =document.createElement("div");
newDiv.setAttribute("id", "map");
newDiv.style.width="100px";
newDiv.style.height="100px";
body.appendChild(newDiv);
};
window.onload=function(){
console.log("ready");
var search_button= document.getElementById("search_button");
search_button.addEventListener("click", dosearch);
}
</script>
<script type="text/javascript">
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDpKAwq-qKxzm-9D1405KCFp7ZTtu_Vimg&callback=initMap"
async defer></script>