Take a look at this code snippet:
I have a function called collection.prototype.add
and I want it to return a reference so that the final alert will show testing, testing, 123, testing
. Any ideas on how to achieve this?
Here is the HTML markup:
<span id="spantest">testing, testing, 123, testing</span>
And here is the JavaScript code:
var collection = function () {
this.items = {};
}
collection.prototype.add = function(sElmtId) {
this.items[sElmtId] = {};
return this.items[sElmtId];
}
collection.prototype.bind = function() {
for (var sElmtId in this.items) {
this.items[sElmtId] = document.getElementById(sElmtId);
}
}
var col = new collection();
var obj = {};
obj = col.add('spantest');
col.bind();
alert(obj.innerHTML);