pebd
pebd

Reputation: 84

How to create a jQuery function (beginner)

I am just starting with jQuery, and am trying to make a function. I would appreciate help with syntax.

http://jsfiddle.net/e2s8besv/

<p>blink me<p>

<style>p {display:none;}</style>

<script>
    (function( $ ) {
        $.fn.blink= function(speed) {
            var speed = $(speed).val();
            $(this).fadeIn(speed).fadeOut(speed).blink(speed);
        };
    }( jQuery));

    $("p").blink(1500);
</script>      

Upvotes: 2

Views: 58

Answers (1)

antyrat
antyrat

Reputation: 27765

You don't need to use val() there:

$.fn.blink= function(speed) {
    $(this).fadeIn(speed).fadeOut(speed).blink(speed);
};

jsFiddle

But I would suggest to return jQuery instance also. So you will be able to reach this element in future or work with elements collection, not first matched element only:

$.fn.blink= function(speed) {
    return this.each( function() {
        $(this).fadeIn(speed).fadeOut(speed).blink(speed);
    });
};

jsFiddle

Upvotes: 2

Related Questions