Create a separate function that will be handling those sections, and call it from the start of each function:
function showSection(name) {
}
$("#about").click(function(){
showSection("about");
$( ".home" ).hide();
});
...
and move in there all of the things that are common to all the other functions:
function showSection(name) {
$( ".home" ).hide();
$( ".headr" ).show();
}
$("#about").click(function(){
// $( ".home" ).hide();
// $( ".headr" ).show();
$( ".about" ).show();
$( ".skills" ).hide();
$( ".experience" ).hide();
$( ".education" ).hide();
$( ".examples" ).hide();
console.log("about clicked");
});
The console.log can be moved in there too, by using the name parameter:
function showSection(name) {
$( ".home" ).hide();
$( ".headr" ).show();
console.log(name + " clicked");
}
$("#about").click(function(){
...
// console.log("about clicked");
});
The rest of the click function is where most of them are hidden, and one thing is shown.
The best solution there is to hide all of them, and then afterwards show something.
function showSection(name) {
$( ".home" ).hide();
$( ".headr" ).show();
$( ".about" ).hide();
$( ".skills" ).hide();
$( ".experience" ).hide();
$( ".education" ).hide();
$( ".examples" ).hide();
console.log(name + " clicked");
}
$("#about").click(function(){
$( ".about" ).show();
// $( ".skills" ).hide();
// $( ".experience" ).hide();
// $( ".education" ).hide();
// $( ".examples" ).hide();
});
That leaves us with the following code:
function showSection(name) {
$( ".home" ).hide();
$( ".headr" ).show();
$( ".about" ).hide();
$( ".skills" ).hide();
$( ".experience" ).hide();
$( ".education" ).hide();
$( ".examples" ).hide();
console.log(name + " clicked");
}
$("#about").click(function(){
showSection("about");
$( ".about" ).show();
});
$("#skills").click(function(){
showSection("skills");
$( ".skills" ).show();
});
$("#experience").click(function(){
showSection("experience");
$( ".experience" ).show();
});
$("#education").click(function(){
showSection("education");
$( ".education" ).show();
});
$("#examples").click(function(){
showSection("examples");
$( ".examples" ).show();
});
We can deal with that show statement by using the name parameter too.
function showSection(name) {
...
$( ".examples" ).hide();
$( "." + name ).show();
console.log(name + " clicked");
}
And now, each click function is nice and simple:
$("#about").click(function(){
showSection("about");
});
$("#skills").click(function(){
showSection("skills");
});
$("#experience").click(function(){
showSection("experience");
});
$("#education").click(function(){
showSection("education");
});
$("#examples").click(function(){
showSection("examples");
});
So simple, that we can now put those names in an array:
function showSection(name) {
...
}
var names = ["about", "skills", "experience", "education", "examples"];
names.forEach(function (name) {
$("#" + name).click(function(){
showSection(name);
});
});
and remove those hide statements from the showSection function.
function showSection(name) {
$( ".home" ).hide();
$( ".headr" ).show();
// $( ".about" ).hide();
// $( ".skills" ).hide();
// $( ".experience" ).hide();
// $( ".education" ).hide();
// $( ".examples" ).hide();
$( "." + name ).show();
console.log(name + " clicked");
}
var names = ["about", "skills", "experience", "education", "examples"];
names.forEach(function (name) {
$("#" + name).click(function(){
names.forEach(function (toHide) {
$("." + toHide).hide();
});
...
});
and extract the remaining contents of the showSection function, back into the click event.
// function showSection(name) {
// $( ".home" ).hide();
// $( ".headr" ).show();
// $( "." + name ).show();
// console.log(name + " clicked");
// }
var names = ["about", "skills", "experience", "education", "examples"];
names.forEach(function (name) {
$("#" + name).click(function(){
$( ".home" ).hide();
$( ".headr" ).show();
names.forEach(function (toHide) {
$("." + toHide).hide();
});
$( "." + name ).show();
console.log(name + " clicked");
});