Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

putting multiple functions under 1 main function

Tags:

javascript

I'm using this line of code quite a bit:

.hide().insertBefore("#placeholder").fadeIn(1000);

Is there a way to make this a function or variable (pretty sure can't use a var, but thought I'd ask) so I can call it as needed? I know I could just copy/paste, but it clutters up the code to see that over and over.

I tried:

function properDisplay() {
    .hide().insertBefore("#placeholder").fadeIn(1000);
}

But that doesn't work.

like image 449
megler Avatar asked Aug 08 '26 13:08

megler


2 Answers

You can make it a plugin:

$.fn.properDisplay = function(){
  return this.hide().insertBefore("#placeholder").fadeIn(1000);
};

Usage:

$('#SomeElement').properDisplay();
like image 189
Guffa Avatar answered Aug 11 '26 01:08

Guffa


You need to pass the element object as parameter

function properDisplay(ele) {
   $(ele).hide().insertBefore("#placeholder").fadeIn(1000);
}
like image 21
Pranav C Balan Avatar answered Aug 11 '26 01:08

Pranav C Balan