Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery functions

I'm trying to write my own function like with no luck. "...is not a function"

jQuery.fn.extend ({
myFirstFunction : function () { alert ("first function") }
});

<script type="text/javascript">
    $(function(){           
              $.myFirstFunction();
    });
</script>
like image 426
ShaneKm Avatar asked Sep 26 '26 18:09

ShaneKm


2 Answers

$.myFirstFunction = function () { alert ("first function") };

<script type="text/javascript">
$(function(){           
          $.myFirstFunction();
});
</script>

http://jsfiddle.net/Mdqw6/

Or

jQuery.fn.extend ({
  myFirstFunction : function () { alert ("first function") }
});

<script type="text/javascript">
    $(function(){           
              $(element).myFirstFunction();
    });
</script>

Read more at http://docs.jquery.com/Plugins/Authoring

like image 190
Jishnu A P Avatar answered Sep 29 '26 08:09

Jishnu A P


There is a difference between $.extend() and $.fn.extend(), depending on what you want to do and how you want to access your function:

$.fn.extend({
    myFunction: function(){...}
});
$('div').myFunction();

$.extend({
    myFunction2: function(){...}
});
$.myFunction2();

In your case, use $.extend() and it should work fine.

like image 26
DarthJDG Avatar answered Sep 29 '26 08:09

DarthJDG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!