fakeguybrushthreepwood
fakeguybrushthreepwood

Reputation: 3083

How to run a nested javascript function?

I am new to object orientated programming in javascript and am trying to understand some functions in a project I am working on.

How would I call/run the internal function (the one listed 'this.getFieldset = function() {') to execute?

function Fieldset() {

    this.id = "";
    this.content = document.createElement("DIV");
    this.content.id = "content";
    this.title = "Title";

    this.getFieldset = function() {

        var div = document.createElement("DIV");
        div.id = this.id;
        var span = document.createElement("SPAN");
        var fieldset = document.createElement("DIV");
        fieldset.id = "fieldset";
        var header = document.createElement("DIV");
        header.id = "header";
        span.appendChild(document.createTextNode(this.title));
        header.appendChild(span);
        div.appendChild(header);
        div.appendChild(this.content);
        div.appendChild(fieldset);

        return div;
    }
}

var myFieldset = new Fieldset();
myFieldset.getFieldset();

Upvotes: 0

Views: 79

Answers (2)

Christopher Marshall
Christopher Marshall

Reputation: 10736

    function Fieldset() {

    this.id = "";
    this.content = document.createElement("DIV");
    this.content.id = "content";
    this.title = "Title";

    this.getFieldset = function() {

        var div = document.createElement("DIV");
        div.id = this.id;
        var span = document.createElement("SPAN");
        //var fieldset = document.createElement("DIV");
        //fieldset.id = "fieldset";
        var header = document.createElement("DIV");
        header.id = "header";
        span.appendChild(document.createTextNode(this.title));
        header.appendChild(span);
        div.appendChild(header);
        div.appendChild(this.content);
        div.appendChild(fieldset);

        window.alert("test");

        return div;
    }

    //add call to run function
    this.getFieldset();

}

Upvotes: -1

Joseph Silber
Joseph Silber

Reputation: 219930

First you should create an instance of Fieldset, then you'll be able to call its functions (called methods):

var myFieldset = new Fieldset();

myFieldset.getFieldset();

Upvotes: 5

Related Questions