JavaScript Elegant way of avoiding writing so many (click hide show)

im looking for the elegant way to avoid writing so much code to perform onclick, show clicked, hide others.

here's the code im using:

html:

<p align="center" style="font-size: 22px;">
  <span class="badge badge-secondary" id="yesterday"><a target='_blank' href="#" style="color: inherit;">Yesterday</a></span>
  <span class="badge badge-dark" id="today"><a target='_blank' href="#" style="color: inherit;">Today</a></span>
  <span class="badge badge-secondary" id="tomorrow"><a target='_blank' href="#" style="color: inherit;">Tomorrow</a></span>
</p>

jquery:

  $('#yesterday').click(function(e) {
    e.preventDefault();
    $(this).addClass('badge-dark').removeClass('badge-secondary');
    $('#today,#tomorrow').addClass('badge-secondary').removeClass('badge-dark');
    $('.yesterday').slideDown('slow');
    $('.today,.tomorrow').slideUp('1000');
  });
  $('#today').click(function(e) {
    e.preventDefault();
    $(this).addClass('badge-dark').removeClass('badge-secondary');
    $('#yesterday,#tomorrow').addClass('badge-secondary').removeClass('badge-dark');
    $('.today').slideDown('slow');
    $('.yesterday,.tomorrow').slideUp('1000');
  });
  $('#tomorrow').click(function(e) {
    e.preventDefault();
    $(this).addClass('badge-dark').removeClass('badge-secondary');
    $('#yesterday,#today').addClass('badge-secondary').removeClass('badge-dark');
    $('.tomorrow').slideDown('slow');
    $('.yesterday,.today').slideUp('1000');
  });
Answer:1

To do this:

  1. Use a class on those three elements (say, show-hide)
  2. Use a class on the .yesterday, .today, and .tomorrow elements as well (say, slide-target).
  3. Use a single click handler on the class
  4. Within the handler, this is the element you want to show, and its siblings (see siblings) are the ones you want to hide
  5. Within the handler, $('.slide-target') is all the targets, then you can use .filter('.' + this.id) to only target the one for this element, and .not('.' + this.id) to target the others

So roughly speaking:

<p align="center" style="font-size: 22px;">
  <span class="show-hide badge badge-secondary"><a target='_blank' href="#" style="color: inherit;">Yesterday</a></span>
  <span class="show-hide badge badge-dark"><a target='_blank' href="#" style="color: inherit;">Today</a></span>
  <span class="show-hide badge badge-secondary"><a target='_blank' href="#" style="color: inherit;">Tomorrow</a></span>
</p>

and

$('.show-hide').click(function(e) {
  e.preventDefault();
  // Just to avoid doing it repeatedly
  var $this = $(this);
  // Add this class
  $this.addClass('badge-dark').removeClass('badge-secondary');
  // Remove it from siblings
  $this.siblings().addClass('badge-secondary').removeClass('badge-dark');
  // Find the target elements
  $('.slide-target')
    .filter('.' + this.id).slideDown('1000').end() // Slide down related
    .not('.' + this.id).slideUp('1000')            // Slide up others
  // Slide down the relevant element(s)
});
Answer:2

A simple extraction of common logic to separate function:

function updateClasses(element, selector) {
    element.addClass('badge-dark').removeClass('badge-secondary');
    $(selector).addClass('badge-secondary').removeClass('badge-dark').slideUp('1000');
}

$('#yesterday').click(function (e) {
    e.preventDefault();
    updateClasses(this, '#today,#tomorrow');
    $('.yesterday').slideDown('slow');
});
$('#today').click(function (e) {
    e.preventDefault();
    updateClasses(this, '#yesterday,#tomorrow');
    $('.today').slideDown('slow');
});
$('#tomorrow').click(function (e) {
    e.preventDefault();
    updateClasses(this, '#yesterday,#today');
    $('.tomorrow').slideDown('slow');
});
Answer:3

Something like this:

$('#yesterday, #today, #tommorow').click(function(e) {

      e.preventDefault(); 
      $(this).addClass('badge-dark').removeClass('badge-secondary');

      if ( $(this).is("#yesterday") ) {

        $('#today,#tomorrow').addClass('badge-secondary').removeClass('badge-dark');
        $('.yesterday').slideDown('slow');
        $('.today,.tomorrow').slideUp('1000');

      } else if ( $(this).is("#today") ) {

        $('#yesterday,#tomorrow').addClass('badge-secondary').removeClass('badge-dark');
        $('.today').slideDown('slow');
        $('.yesterday,.tomorrow').slideUp('1000');

      } else if ( $(this).is("#tomorrow") ) {

        $('#yesterday,#today').addClass('badge-secondary').removeClass('badge-dark');
        $('.tomorrow').slideDown('slow');
        $('.yesterday,.today').slideUp('1000');

      }

    });
Answer:4

I apply inheritance in JavaScript in following way: var employee = function(name) { this.name = name; } employee.prototype.getName = function() { return this.name; } var pEmployee = ...

I apply inheritance in JavaScript in following way: var employee = function(name) { this.name = name; } employee.prototype.getName = function() { return this.name; } var pEmployee = ...

  1. inherit javascript function
  2. javascript inheritance function override
  3. odoo inherit javascript function
  4. javascript inheritance extend function
  5. javascript inheritance super function
  6. javascript function inheritance prototype
  7. javascript inheritance call parent function
  8. javascript inheritance function call

I'm using angular-ui-tree in order to render a tree with data but each time that the page is loaded all nodes are rendered and I would like to avoid the render because with huge data locks the browser....

I'm using angular-ui-tree in order to render a tree with data but each time that the page is loaded all nodes are rendered and I would like to avoid the render because with huge data locks the browser....

  1. angular avoid tree

I am trying to get the value of some array elements. It works for the elements [0], [1], [2], [3], but not [4]. function getBase64() { const urls = ['https://i.imgur.com/egNg7JU.jpg', 'https://...

I am trying to get the value of some array elements. It works for the elements [0], [1], [2], [3], but not [4]. function getBase64() { const urls = ['https://i.imgur.com/egNg7JU.jpg', 'https://...

  1. javascript wait array of promises
  2. javascript wait array
  3. javascript wait until array is filled
  4. javascript wait for array to be populated
  5. javascript wait for array of promises
  6. js wait for array of promises

I'm struggling to figure out how we're supposed to handle submitting a form in React. First time user, failed hard so far. The data in the form is always empty meaning the json is also empty. As far ...

I'm struggling to figure out how we're supposed to handle submitting a form in React. First time user, failed hard so far. The data in the form is always empty meaning the json is also empty. As far ...

  1. react form submission tutorial
  2. react form submission canceled not connected
  3. react form submission example
  4. react form submission
  5. react form submit
  6. react form submit example
  7. react form submit on enter
  8. react form submit get values
  9. react form submit prevent default
  10. react form submit reloads page
  11. react form submit button
  12. react form submit redirect
  13. react form submit refreshes page
  14. react form submit values
  15. react form submit to api
  16. react form submit typescript
  17. react form submit without refresh
  18. react form submit preventdefault not working
  19. react form submit function
  20. react form submit no refresh