3
<script type="text/javascript" src="jscripts/jquery.js"></script>

<script type="text/javascript">
$(document).ready(function(){
alert("funciton");
    $(function(){
        $.fn.gotof(){
            alert("I am calling form jquery");
        }           
    });
});
</script>


<input type="button" onclick="dofunc();">

<script type="text/javascript">
    function dofunc(){
        gotof();
    }
</script>

how do i call gotof() that is present in jquery and below is the code written over jsfiddle

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
Rafee
  • 3,975
  • 8
  • 58
  • 88
  • 3
    Your syntax is flawed. Why do you want `$.fn` anyway if you want to call `gotof()` regularly? I don't see the necessity for jQuery here. – pimvdb Dec 16 '11 at 11:21
  • 1
    You created a jQuery plugin (though in a unnecessary complex way). `gotof` will be available as `$(selector).gotof()`. Maybe have a look at http://docs.jquery.com/Plugins/Authoring and jQuery/JavaScript in general again. *Edit:* Well, if you fix the syntax errors ;) – Felix Kling Dec 16 '11 at 11:21
  • 2
    I'm really not sure what you are trying to do here, but `$.fn.gotof(){` is a syntax error. – Quentin Dec 16 '11 at 11:22
  • You are subscribing to the document load function inside of document load function. It seems that your code will be never invoked. – Samich Dec 16 '11 at 11:23
  • 1
    @Samich: That's because of the syntax errors. jQuery makes sure that those callbacks are called correctly. – Felix Kling Dec 16 '11 at 11:24
  • @FelixKling Yep, just checked it.. Thanks for clarification – Samich Dec 16 '11 at 11:28
  • jQuery _is_ JavaScript. (Somebody had to point it out.) Beyond syntax technicalities is there a particular purpose behind this? – nnnnnn Dec 16 '11 at 11:47

2 Answers2

8

There are a few errors in your code. Fixed it should look like this:

$.fn.gotof = function() { // has to be defined as a function, does not need to be inside a nested document ready function
    alert("I am calling form jquery");
};

$(document).ready(function() {
    alert("function is ready to use now");
});

function dofunc() {
    $.fn.gotof();  // can call it using $.fn.gotof(), but it should really be called properly via a selector $('div').gotof();
}

http://jsfiddle.net/pSJL4/8/

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
0

Check out http://docs.jquery.com/Plugins/Authoring - this should answer your questior. You also are not defining your function correctly; instead of

$.fn.gotof(){
            alert("I am calling form jquery");
        }

you need

$.fn.gotof = function(){
            alert("I am calling from jquery");
        };
Graham
  • 6,484
  • 2
  • 35
  • 39