Currently, I am delving into the world of JavaScript and experimenting with various thingamajigs. Could someone kindly shed some light on why my script is throwing errors?
// Effects object
var effects = {
// Display an object
show : function(obj) {
obj.style.display = 'block';
},
// Hide an object
hide : function(obj) {
obj.style.display = 'hide';
},
// Toggle
toggle : function(obj) {
if (obj instanceof Array) {
alert('array');
} else {
alert('single');
}
}
}
// Selector Class
var s = {
id : function(name) {
return document.getElementById(name);
},
class : function(name) {
node = document.getElementsByTagName("body")[0];
var a = [];
var re = new RegExp('\\b' + classname + '\\b');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))
a.push(els[i]);
return a;
}
}
window.onload = function() {
s.id('toggle-content').onclick(function() {
effects.toggle(s.class('hidden-content'));
});
}
The following errors are popping up:
Upon page load:
Error: s.id("toggle-content").onClick is not a function Source File: http://localhost/cms/web/js/admin.js Line: 40
By the way, is there a standardized method to fetch a class from the dom efficiently?
Mystery solved!
Replace onClick() with onclick= instead.