Luarb Balla Luarb Balla - 5 months ago 15
jQuery Question

How to use custom function in jquery chaining

Is it possible to make something like this:

function hideThisObject(objectName){
$(objectName).css({
"transition":"200ms",
"opacity":"0"
});
setTimeout(function(){
$(objectName).remove();
},250);
}

$('p').hideThisObject(this);

Answer

You need to use jquery $.fn.* to declaring jquery custom function.

$.fn.customFunc = function(){
    $(this).css("background-color", "green");
};

$("div").customFunc();
div {
    width: 100px;
    height: 100px;
    background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

Comments