I am new to jQuery and needed a solution for the following problem:
There are 3 images in a row and when the user clicks on one of them all three pictures are hiding and instead another image shows up (depending on the click). So while the markup and CSS is quite simple and only a quick draft the jQuery was the main issue. I came up with the following solution and would love to hear some advice, reviews, critiques or similar.
I also made a JSFiddle. for better understanding.
Here is my jQuery Code with similar click-functions (this bothers me a little):
var $button = '<button id="closebtn">Close</button>';
$($button).appendTo('#panzer');
// Pic1 hide and replace
$( "#pic1" ).click(function() {
$('#featured li img').hide( "slow");
$( ".undercon1" ).show( "fast", function() {
$('#closebtn').show();
});
});
// Pic2 hide and replace
$( "#pic2" ).click(function() {
$('#featured li img').hide( "slow");
$( ".undercon2" ).show( "fast", function() {
$('#closebtn').show();
});
});
// Pic3 hide and replace
$( "#pic3" ).click(function() {
$('#featured li img').hide( "slow");
$( ".undercon3" ).show( "fast", function() {
$('#closebtn').show();
});
});
// Button shows only with Replacement Images
$('button#closebtn').click(
function ()
{
$(this).hide();
$(".undercon1").hide( "slow");
$(".undercon2").hide( "slow");
$(".undercon3").hide( "slow");
$('#featured li img').show( "slow");
}
);
div
is not allowed as direct child of aul
\$\endgroup\$