I am trying to make a basic JavaScript 'library' / tool which helps the user change items using their IDs. I have tried a basic function, but I get the error Uncaught TypeError: Object [object global] has no method 'val', and I am not sure why:
lib.js
function $id(id)
{
if(id)
{
this.elem = document.getElementById(id);
return this;
}
else
{
console.log("No ID found when using $id(...)");
}
}
$id.prototype =
{
val: function()
{
return this.elem.value;
},
set: function(v)
{
this.elem.value = v;
},
destroy: function()
{
this.elem.parentNode.removeChild(this.elem);
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="lib.js"></script>
<script type="text/javascript">
alert($id("myDiv").val());
</script>
</head>
<body>
<div id="myDiv">
Hello
</div>
</body>
</html>