tags:

views:

21

answers:

2

I want to create a plugin 'myPlugin' which simply add some text to a div. like:

 document.getElementById('testDiv').myPlugin("this is a text");

how can I achieve this through singleton method as well as prototype method ?

A: 
HTMLElement.prototype.myPlugin = function(t) {
    // ...
}

Will add a method to all HTML elements in compliant browsers. It won't work on IE though (at least on the old versions, I honestly haven't tried in IE8).

What do you mean by "singleton method"?

Matti Virkkunen
Hi matti, thanks for your replay.I don't want to extend the HTMLElement object. I want something like:function myPlugin() {this.create = function(text){ div.innerHTML = text;};}or var myPlugin = { create:function(text){ div.innerHTML = text; }}I'm basically a designer, not much programming knowlwdge.
Aneesh
And... the code you just posted doesn't do what you'd like it to do?
Matti Virkkunen
A: 
function appendTextToElement(element, text) {
  var textProperty = element.innerText === undefined ? "textContent" : "innerText";
  element[textProperty] = element[textProperty] + text;
}

And a test.

Alsciende