javascript - What is the most elegant way to insert this conditional statement? -
i pass variable either cause method click
or hover
.
right now, defaults click.
var defaults = { xoffset: 10, yoffset: 25, tooltipid: "easytooltip", clickremove: true, content: "", useelement: "", clickappear: true }; var options = $.extend(defaults, options); var content; $(this).click(function(e){ $("#" + options.tooltipid).remove(); content = (options.content != "") ? options.content : title; content = (options.useelement != "") ? $("#" + options.useelement).html() : content; $(this).attr("title",""); if (content != "" && content != undefined){ $("body").append("<div id='"+ options.tooltipid +"'>"+ content +"</div>"); $("#" + options.tooltipid) .css("position","absolute") .css("top",(e.pagey - options.yoffset) + "px") .css("left",(e.pagex + options.xoffset) + "px") .css("display","none") .fadein("fast"); return false; } },
i want pass in method call clickappear = false
, in case alternatively produce this:
$(this).hover(function(e){ ...
instead of copying , pasting entire method twice in conditional ( didn't seem work anyway ), wondering if there elegant way write conditional while keeping block dry.
any ideas?
well can write:
$(this)[options.clickappear ? "click" : "hover"](function(e) { // code });
that checks option , picks either "click" or "hover" function, , calls it.
Comments
Post a Comment