Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function inside function through only HTML

I just can't reach the function inside function using only HTML. How to call setLayout() using only HTML or is it able to call only in Javascript?

<button onclick="customize.setLayout('b.html');">Click Please</button>

Javascript:

 function customize() {
     function setLayout(text) {

        var selectedLayout = text;
        layout += selectedLayout;
        $.get(layout, function (data) {
            $("#layout-grid").html(data);
        });
    }
}
like image 842
Mes Avatar asked Dec 25 '15 08:12

Mes


People also ask

How do you call a function inside a function in HTML?

In this method, we will create and define a function in the HTML document's head section. To invoke this function in the html document, we have to create a simple button and using the onclick event attribute (which is an event handler) along with it, we can call the function by clicking on the button.

Can you call a function inside of a function?

Calling a function from within itself is called recursion and the simple answer is, yes.

Can I use function in HTML?

The first method is to call the JavaScript function in HTML. For this, you have to create a function then define this function either in the head section or body section of the HTML document. You can either create a link or a button and then an onclick() event is associated with them in order to call this function.


2 Answers

It isn't possible to call setLayout at all.

Functions defined in other functions are scoped to that function. They can only be called by other code from within that scope.

If you want to to be able to call customize.setLayout then you must first create customize (which can be a function, but doesn't need to be) then you need to make setLayout a property of that object.

customize.setLayout = function setLayout(text) { /* yada yada */ };
like image 195
Quentin Avatar answered Sep 28 '22 16:09

Quentin


Multiple ways to call a function within a function. First of all, the inner function isn't visible to the outside until you explicitly expose it Just one way would be:

function outerobj() {

    this.innerfunc = function () { alert("hello world"); }

}

This defines an object but currently has no instance. You need to create one first:

var o = new outerobj();
o.innerfunc();

Another approach:

var outerobj = {

    innerfunc : function () { alert("hello world"); }

};

This would define an object outerobj which can be used immediately:

outerobj.innerfunc();
like image 32
Axel Amthor Avatar answered Sep 28 '22 14:09

Axel Amthor