Sorry , this might be a stupid question but I dont really get why this won't work for me. I just want to be able to write the result from myitem in to a simple div. I have used a function onmouseover to get it to work. but that can't be neccessary. And i know how to show it in an alert.
But how do one get myitem to show in a div?
help is much appreciated!:)
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="utf-8">
<title> class </title>
<script>
class item {
constructor(name, price, quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
sum() {
return this.price * this.quantity;
}
}
myitem = new item("apple", 10, 2);
// This doesnt work // document.getElementById("example1").innerHTML = myitem.sum();
// This doesnt work // document.getElementById("example1").innerHTML = (myitem.sum());
//this down below works
function myfunction () {
document.getElementById("example2").innerHTML = myitem.sum();
}
// This works // alert(myitem.sum());
</script>
</head>
<body>
<div id="example1"> example1 </div>
<br/>
<div id="example2" onmouseover="myfunction();"> example2 </div>
</body>
</html>