You can *NOT* just ARBITRARILY apply a method to a DOM object. Or to any object, for that matter.
*IF* the object in question was a true JavaScript object, you could alter its prototype to add the method. But because it is a DOM object, and because the DOM is *ACTUALLY* implemented in native code under the covers, not at all in JavaScript, there is no way to directly add methods to DOM objects.
You *could* create a JavaScript "wrapper" object for your DOM objects. You might, for example, create one wrapper for all the DOM objects that indeed have an innerHTML property (you'd surely want a different wrapper for fields that instead have a value property, for example).
Maybe something like this:
Code:
// constructor for a wrapper for an object with innerHTML property:
function htmlObj( objOrId )
{
// if a string is passed as the argument, assume it is an ID
if ( typeof(objOrId) == "string" ) {
this.domObj = document.getElementById(objOrId);
} else {
// otherwise assume it's a DOM object
// (not the best assumption to make, but this is a silly demo)
this.domObj = objOrId;
}
this.html = function( html )
{
// if the argument is non-null, change the DOM object's innerHTML
if ( html != null ) { this.domObj.innerHTML = html; }
// and not matter what, always return its innerHTML
return this.domObj.innerHTML;
}
}
var box1 = new htmlObj( "divBox1" );
box1.html( "some html content" ); // pass non-null to set the innerHTML
// or
alert( box1.html() ); // pass null or no arg to GET the innerHTML
Truthfully, I think that's more trouble than it is worth, but up to you.