homepage Welcome to WebmasterWorld Guest from 54.198.42.213
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
jQuery function
how to simplify this function
drooh




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

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

 

daveVk




msg:4575937
 11:58 pm on May 20, 2013 (gmt 0)

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');

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved