6

As the title says, I have exactly the same code needs to run in both functions. Is there a way to put them together and run as one function please? Thanks!

Stickers
  • 75,527
  • 23
  • 147
  • 186

2 Answers2

25

Yes. Simply define a function with the common code and pass that as the argument to the two methods.

var callback = function () {
  // This is the common code
};

$(document).ready(callback);
$(window).resize(callback);
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1
$(window).on('resize',function() {

}).trigger('resize');
davidcondrey
  • 34,416
  • 17
  • 114
  • 136