You can extend the jQuery library as follows:
$(function () { $.fn.afterif = function (param, condition) { if (condition) { $(this).after(param); } return this; } }(jQuery));
And use it as follows:
var insertAfter = true; $('#whatEver').afterif($('<div />'), insertAfter);
source share