0

How to call a method from the javascript class from html?

I not find solution for to call class method from html generated code using javascript.

I write small example for better understand.

<head>
    <script>
        function MyClass() {
            var self = this;

            this.init = function() {
            document.getElementById('list').innerHTML = this.mainTemplate();
            };

            this.mainTemplate = function () {
                return '<button id="start" class="btn btn-danger btn-lg" onclick="onStart()" type="button"> value </button>';
            };

            this.onStart = function onStart() {
                alert('bingo');
            };
        }
    </script>
</head>
<body>
    <div id ="list"> </div>
        <script>
            var a = new MyClass();
                a.init();
                //a.onStart();
        </script>
</body>

I have tried everything that comes to mind, like ...

  1. onStart()
  2. self.onStart()
  3. this.onStart()
  4. MyClass.onStart()
  5. MyClass.prototype.onStart()
  6. a.onStart
  7. MyClass.onStart
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
magjakov
  • 13
  • 1
  • 2

3 Answers3

2

onclick="onStart()" is not referencing a.onStart() , but this at onStart() would be window.onStart(), which if exists is not a.onStart.

You can substitute .addEventListener() for onclick event attribute attached to #start element at init() to add the click event to #start element after the element has been appended to document.

<head>
  <script>
    function MyClass() {
      var self = this;

      this.init = function() {        
        document.getElementById('list').innerHTML = this.mainTemplate();
        // attach `click` event to `#start` here, 
        // set `this.onStart` as `click` handler function reference
        document.getElementById("start").addEventListener("click", this.onStart) // or `self.onStart`
      };

      this.mainTemplate = function() {
        return '<button id="start" class="btn btn-danger btn-lg" '
               + 'type="button"> value </button>';
      };

      this.onStart = function onStart() {
        alert('bingo');
      };
    }
  </script>
</head>

<body>
  <div id="list"></div>
  <script>
    var a = new MyClass();
    a.init();
  </script>
</body>
guest271314
  • 1
  • 15
  • 104
  • 177
-1

You can bind to the body tag's load event pretty easily:

<body onload="a.onStart()">

It will wait until all the HTML has loaded in the body tag, but it wont wait for other files like images etc.

The object you want to use must be prepared first though, so move that script tag from the bottom into the head

However, I think I kind of missed the point. It seems like you want the event to fire when the button is clicked, not when the page loads.

In that case, it would be easier to manage later if you create an element and append it to an existing element instead of creating a string and replacing the html content of an existing element:

    <script>
    function MyClass() {
        var self = this;

        this.init = function() {
            document.getElementById('list').appendChild(this.mainTemplate());
        };

        this.mainTemplate = function () {
            button = document.createElement('button');
        //  button.id = "start";
            button.className = "btn btn-danger btn-lg";
            button.type = "button";
            button.innerHTML = " value ";
            button.addEventListener("click", this.onStart);

            return button;
        }
        this.onStart = function() {
            alert('bingo');
        };
    }

    </script>
    <div id ="list"> </div>
    <script>
        var a = new MyClass();
            a.init();
        //    a.onStart();
    </script>

http://codepen.io/t3hpwninat0r/pen/ONQQBP

This method allows multiple instances of the class with their own button.

vahanpwns
  • 939
  • 5
  • 12
-1

Change the line:

            this.onStart = function onStart() {

to just say:

        this.onStart = function() {

then call it with a.onStart() and it will work.

jimboweb
  • 4,362
  • 3
  • 22
  • 45
  • Explain the downvote? As far as I can tell, the person asked how to call the onStart function and I answered that question. – jimboweb Apr 11 '16 at 19:48
  • I've just tested this and it works. why the downvote? http://codepen.io/t3hpwninat0r/pen/GZQQBV note: the button has `a.onStart()` on click – vahanpwns Apr 11 '16 at 19:48