JQuery Object Context of Function

From CoderLearner
Jump to: navigation, search

Description

A function that can be either named or anonymous. Function can passed to a method or assign to a variable.

Syntax


function named() {
}

var myVariable= function() {
}

Example

<html>
    <head>
        <title>Object</title>
        <script type="text/javascript" src="/js/jquery-1.8.3.js"></script>
        <script type="text/javascript">
            $().ready(function() {
                var get_a = function(theText){
                    $('#show_text').html(theText);
                };
                get_a("This is my Function Testing");
                
            });
        </script>
    </head>
    <body> 
        <div id="show_text"></div>
    </body>
</html>

Result

This is my Function Testing

Example 2

<html>
    <head>
        <title>Object</title>
        <script type="text/javascript" src="/js/jquery-1.8.3.js"></script>
        <script type="text/javascript">
            $().ready(function() {
                var make_list = function(ArrayList, assingID) {
                    var tmp = "";
                    $.each(ArrayList, function(key, value) {
                        console.log(value);
                        tmp += "<li>" + value + "</li>";
                    });
                    assingID.html(tmp);
                };

                make_list(["Houston", "Dallas", "New Orlean", "Huntsville"], $('#myList'));
            });
        </script>
    </head>
    <body> 
        <ul id="myList"></ul>
    </body>
</html>

Result

Houston
Dallas
New Orlean
Huntsville