function crud(params, url, type, callback) { $.ajax({ type: type, data: params, url: url, success: function (response) { console.log(response) callback(response); }, }); } function getMyMessages(type) { crud({'type': type}, '/api/messages', 'GET', function (response) { $('#nav-' + type).html(""); $.each(response, function (index, message) { $('#nav-' + type).append( addMessagesRow( message.id, message.sender_name, message.receiver_name, message.sent_at ) ); }); }) } $('.profile-ads-tab a').on('click', function () { getMyMessages($(this).attr('data-type')) }); getMyMessages('inbox'); function addMessagesRow(id, senderName, receiverName, sentAt) { return `
${from}:${senderName}
${to}:${receiverName}
${sentAt}
` }