javascript - Simplifying a jQuery functions -
i wondering method of simplifying script, because somehow repeating myself on again...
$('.userprofile').click(function(){ card_profile.load(url_settings).dialog('open'); }); $('.cust-profile').click(function(){ card_profile.load(url_customer).dialog('open'); }); $('.my-profile').click(function(){ card_profile.load(url_my).dialog('open'); });
var obj = { '.userprofile' : url_settings, '.cust-profile': url_customer, '.my-profile' : url_my }; $.each(obj, function(sel, url) { $(sel).click(function(){ card_profile.load(url).dialog('open'); }); });
or
$(".userprofile,.cust-profile,.my-profile").click(function() { var url = $(this).hasclass("userprofile") ? url_settings : $(this).hasclass("cust-profile") ? url_customer : url_my; card_profile.load(url).dialog("open"); });
Comments
Post a Comment