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

7
  • 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. Commented Dec 16, 2011 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 docs.jquery.com/Plugins/Authoring and jQuery/JavaScript in general again. Edit: Well, if you fix the syntax errors ;) Commented Dec 16, 2011 at 11:21
  • 2
    I'm really not sure what you are trying to do here, but $.fn.gotof(){ is a syntax error. Commented Dec 16, 2011 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. Commented Dec 16, 2011 at 11:23
  • 1
    @Samich: That's because of the syntax errors. jQuery makes sure that those callbacks are called correctly. Commented Dec 16, 2011 at 11:24

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

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");
        };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.