iJade
iJade

Reputation: 23801

Issue Calling jquery function on button click

Here is my jquery code

(function($){
var W=$(window),
    D=$(document),
    Ht = D.height(),
    Wt = W.width();
$.fn.ThrowMask = function(){
             //Working code
        }
});

i tried to attach this code on button click like this

<script type="text/javascript">
    $(document).ready(function(){
        $("#btn").click(function(){
            ThrowMask();
        });

    });
</script>

But this causes error undefined.

Upvotes: 0

Views: 2461

Answers (3)

Jarod Law Ding Yong
Jarod Law Ding Yong

Reputation: 5757

This is the working code:

(function($){
var W=$(window),
    D=$(document),
    Ht = D.height(),
    Wt = W.width();
$.fn.ThrowMask = function(){
             //Working code which binding click event
             $(this).click(function (event) {
                  // do your click even handler here
             });

        }
}(jQuery));



<script type="text/javascript">
    $(document).ready(function(){
        $("#btn").ThrowMask();

    });
</script>

Upvotes: 0

Isaac Fife
Isaac Fife

Reputation: 1689

You never call the lambda function. use this instead

(function($){
var W=$(window),
    D=$(document),
    Ht = D.height(),
    Wt = W.width();
$.fn.ThrowMask = function(){
             //Working code
        }
})($);

Notice the two extra parens. Added jquery to function call.

Upvotes: 1

Kevin B
Kevin B

Reputation: 95017

In addition to Isaac Fife's fix, you need to call the function properly.

<script type="text/javascript">
    $(document).ready(function(){
        $("#btn").click(function(){
            $(this).ThrowMask();
        });
    });
</script>

Upvotes: 2

Related Questions