I am hiding and fading in different content on the same page using jQuery to hide()
and fadeIn()
the content depending on which link is clicked.
It works how I want it to, but the way I've written the jQuery looks like it could be simplified.
$("#navItem1").on('click', function(){
$('#content-wrap1').hide();
$('#content-wrap2').hide();
$('#content-wrap3').hide();
$('#content-wrap4').hide();
$('#content-wrap5').hide();
$('#content-wrap1').fadeIn(1000);
});
$("#navItem2").on('click', function(){
$('#content-wrap1').hide();
$('#content-wrap2').hide();
$('#content-wrap3').hide();
$('#content-wrap4').hide();
$('#content-wrap5').hide();
$('#content-wrap2').fadeIn(1000);
});
$("#navItem3").on('click', function(){
$('#content-wrap1').hide();
$('#content-wrap2').hide();
$('#content-wrap3').hide();
$('#content-wrap4').hide();
$('#content-wrap5').hide();
$('#content-wrap3').fadeIn(1000);
});
$("#navItem4").on('click', function(){
$('#content-wrap1').hide();
$('#content-wrap2').hide();
$('#content-wrap3').hide();
$('#content-wrap4').hide();
$('#content-wrap5').hide();
$('#content-wrap4').fadeIn(1000);
});
$("#navItem5").on('click', function(){
$('#content-wrap1').hide();
$('#content-wrap2').hide();
$('#content-wrap3').hide();
$('#content-wrap4').hide();
$('#content-wrap5').hide();
$('#content-wrap5').fadeIn(1000);
});
function fadeInFirstContent(){
$('#content-wrap1').fadeIn(1000);
}
fadeInFirstContent();
How can I rewrite the jQuery so it does exactly the same thing but in much less code?
[id]
s. It makes the HTML more expressive of the fact that these elements obviously share a similar class. \$\endgroup\$