user3439585
user3439585

Reputation: 87

jQuery | simple function not working? self invoked function

please take a look at this jsfiddle: click

(function() {
    var variable = {

        body : $('body'),

        bg: function() {
            body.css('background', 'green')
        }

    };
})();

the console doesn't give back any error.

an short explanation would be great whats wrong here!

Thanks!

Upvotes: 0

Views: 35

Answers (2)

petres
petres

Reputation: 582

Look at: http://jsfiddle.net/g3r01zmr/

(function() {
	var variable = {
		body : $('body'),
		bg: function() {
			this.body.css('background', 'green')
		}
	};
    variable.bg()
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

You have to call the function, you only have defined it so far. Also you have to use this for the body variable, because it is not a local variable.

Upvotes: 1

M. Page
M. Page

Reputation: 2814

You don't call bg. You can do:

(function() {
    var variable = {

        body : $('body'),

        bg: function() {
            this.body.css('background', 'green')
        }

    };
    variable.bg();
})();

http://jsfiddle.net/13dvxsnf/1/

Upvotes: 1

Related Questions