Saturday, 14 September 2013

avoiding a duplicate $(document).ready(function() tag

avoiding a duplicate $(document).ready(function() tag

I am at a loss to explain why this snippet of code only works for the
first function. The image swap fails to execute.
$(document).ready(function() {
$(function(){
// tip
$("#tip").fancybox({
'width' : 420,
'height' : 300,
'autoScale' : true,
'autoDimensions' : true,
'transitionIn' : 'elastic',
'transitionOut' : 'fade',
'overlayColor' : '#111',
'type' : 'iframe'
});
});
// image swap
$("#image1, #image2").click(function(){
switch(this.id)
{
case 'image1':
_(this.id,"../../img/answers.png","../../img/b13a(AC)ans.png");
break;
case 'image2':
_(this.id,"../../img/answers(biggest4).png","../../img/b13b(AC)ans.png");
break;
}
function _(id,main, alt){
if($("#"+id).attr("src") == main) $("#"+id).attr("src", alt);
else $("#"+id).attr("src", main);
}
});
If I place the second function within a separate
$(document).ready(function(), both routines work, but I'm trying to avoid
the duplication.

No comments:

Post a Comment