3
\$\begingroup\$

My code works perfectly but it is redundant. How does one condense this? How do you use the =! operator? Any help would be greatly appreciated.

$("#about").click(function(){
 $( ".home" ).hide();
 $( ".headr" ).show();
 $( ".about" ).show();
 $( ".skills" ).hide();
 $( ".experience" ).hide();
 $( ".education" ).hide();
 $( ".examples" ).hide();
 console.log("about clicked");
});
$("#skills").click(function(){
 $( ".home" ).hide();
 $( ".headr" ).show();
 $( ".about" ).hide();
 $( ".skills" ).show();
 $( ".experience" ).hide();
 $( ".education" ).hide();
 $( ".examples" ).hide();
 console.log("skills clicked");
});
$("#experience").click(function(){
 $( ".home" ).hide();
 $( ".headr" ).show();
 $( ".about" ).hide();
 $( ".skills" ).hide();
 $( ".experience" ).show();
 $( ".education" ).hide();
 $( ".examples" ).hide();
 console.log("experience clicked");
});
$("#education").click(function(){
 $( ".home" ).hide();
 $( ".headr" ).show();
 $( ".about" ).hide();
 $( ".skills" ).hide();
 $( ".experience" ).hide();
 $( ".education" ).show();
 $( ".examples" ).hide();
 console.log("education clicked");
});
$("#examples").click(function(){
 $( ".home" ).hide();
 $( ".headr" ).show();
 $( ".about" ).hide();
 $( ".skills" ).hide();
 $( ".experience" ).hide();
 $( ".education" ).hide();
 $( ".examples" ).show();
 console.log("examples clicked");
});
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Aug 31, 2018 at 21:20
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

1) Add another common class (using toggle in the example) to all of your tags

<div class="toggle home">data</div>
<div class="toggle headr">data</div>
<div class="toggle about">data</div>
<div class="toggle skills">data</div>
<div class="toggle experience">data</div>
<div class="toggle education">data</div>
<div class="toggle examples">data</div>

2) on your click handler add data tags (using data-show in this example) for what you want to show

<button data-show="home">home</button>
<button data-show="headr">header</button>
<button data-show="about">about</button>
<button data-show="skills">skills</button>
<button data-show="experience">experience</button>
<button data-show="education">education</button>
<button data-show="examples">examples</button>

3) Set up one function to handle all clicks

$('button').click(function(event) {
 $('.toggle').hide();
 $('.' + event.currentTarget.data.show).show();
 console.log(event.currentTarget.data.show + ' clicked');
});
answered Aug 31, 2018 at 21:38
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You probably want to use event.currentTarget.dataor this.data` instead of event.data. \$\endgroup\$ Commented Sep 1, 2018 at 0:08
0
\$\begingroup\$

A sugestion I can give to you is to cache your DOM elements like this:

var about = $('#about');
var home = $('.home'):

ecc.. Using this way you can have a code more readable

about.click(function(){
 home.hide();
 ecc..
 console.log("about clicked");
});

Another suggestion as mentioned in the above answer is to create a class that you can add to the elements you need to hide.

<button class=".hidden">home</button>
<button class=".visible">about</button>
.hidden{
 display: none;
}
.visible{
 display: block;
}
home.on('click', function(e){
 about.toggleClass('hidden');
});
answered Sep 1, 2018 at 16:47
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.