Welcome to WebmasterWorld Guest from 54.162.93.137

Forum Moderators: open

Message Too Old, No Replies

jQuery function

how to simplify this function

     
3:59 pm on May 20, 2013 (gmt 0)

5+ Year Member



So I have this function

$("#brewhouse").click(function (event) {
event.preventDefault();
$('#pLoaderImg').css("display", "block");
$.ajax({
url: "portfolio/brewhouse.php",
dataType: "html",
success: function(data){
$("#webDetail").fadeOut(function() {
$(this).html(data).slideDown('slow');
});
$('#pLoaderImg').fadeOut();
}
});
});


Wondering how I can reuse this function without having to rewrite it each time. For example, everything stays the same except for the url of the loaded php file and the click element (#brewhouse)

should I have a smaller function that calls this function and passes a few variables? just not sure how to do that with jQuery
11:58 pm on May 20, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



enclose what you have within a function

function setClick( elId, url ) {
$('#'+elId).click(function (event) {
event.preventDefault();
$('#pLoaderImg').show();
$.ajax({
url: url,
dataType: "html",
success: function(data){
$("#webDetail").fadeOut(function() {
$(this).html(data).slideDown('slow');
});
$('#pLoaderImg').fadeOut();
}
});
});
);

and call for each
setClick('brewhouse', 'portfolio/brewhouse.php');
 

Featured Threads

Hot Threads This Week

Hot Threads This Month