Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery plugin $.fn.myplugin not working

Tags:

jquery

plugins

I might be making a very simple mistake but am having some serious trouble tring to figure out why it's not working.

Here's the code: http://jsfiddle.net/HthCa/

UPDATE: this is my actual code..

<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript">
    $(function() {
        $('#test').customTabs();
    })
</script>

scripts.js

$.fn.customTabs = function() {
    alert($(this).html());
}
like image 908
Noah Passalacqua Avatar asked Mar 07 '26 03:03

Noah Passalacqua


2 Answers

In your code:

$('#test').customTabs();

$.fn.customTabs = function() {
    alert($(this).html());
};

You're calling $.fn.customTabs() before defining it. Try instead:

$.fn.customTabs = function() {
    alert(this.html());
};

$('#test').customTabs();

Note that you do not need to apply $ to this in a plugin method, as this is already a jQuery object (the one on which the method was called).

like image 155
Frédéric Hamidi Avatar answered Mar 09 '26 16:03

Frédéric Hamidi


Put the new function definition above where you call it.

$.fn.customTabs = function() {
    alert($(this).html());
};
$('#test').customTabs();
like image 21
John Koerner Avatar answered Mar 09 '26 18:03

John Koerner