JavaScript Show content only from the clicked link and hide all siblings show content only on mobile,show content only on homepage

as stated in the name, i have a menu with links, and i have a list of sections which i want to show/hide on the click of the menu. What i want here is to be dynamic in a sense that if i add more menus and sections I don't have to change the code that does it, or to add new tags or names. I tried doing something myself but I'm probably missing something.. Any assistance would be appriciated

I have a simple example on this jfiddle: https://jsfiddle.net/s07ysx6w/6/

HTML:

<div id="header">
   <div id="menuBlock">
    <ul id="menu">
      <li><a target='_blank' href="#novosti" name="sekcija1">Novosti</a></li>
      <li><a target='_blank' href="#programMladi" name="sekcija2">Program Mladi</a></li>
      <li><a target='_blank' href="#programOdrasli" name="sekcija3">Program Odrasli</a></li>
      <li><a target='_blank' href="#programUpisi" name="sekcija4">Program Upisi</a></li>
      <li><a target='_blank' href="#galerija" name="sekcija5">Galerija</a></li>
      <li><a target='_blank' href="#kontakt" name="sekcija6">Kontakt</a></li>
    </ul>
  </div>
</div>

<body>
  <div id="main">
    <div id="novosti" name="sekcija1" class="sekcija">
      aaaa
    </div>
    <div id="programMladi" name="sekcija2" class="sekcija">
      aaaa
    </div>
    <div id="programOdrasli" name="sekcija3" class="sekcija">
      aaaa
    </div>
    <div id="programUpisi" name="sekcija4" class="sekcija">
      aaa
    </div>
    <div id="galerija" name="sekcija5" class="sekcija">
      aaaa
    </div>
    <div id="kontakt" name="sekcija6" class="sekcija">
      aaa
    </div>
  </div>
</body>

Javascript:

$(document).ready(function() {
  $("#menu").click(function(e) {
    var selected = this.attr('href');
    $('#main' + selected).show('slow').siblings().hide('slow');
  });
});

EDITED: Copy/pasting made me careless so now there are only unique id's. Also replaced the fiddle with a working one (solution).

UPDATE: In case anyone uses slicknav as a plugin on his/her's page, to get to the element you have in your menu you need to find how exactly slicknav injected it into your page. For instance, in my case, since i prepend it to my #menuBlock div tag. In order to find the element #novosti i had to dig in deep, since slicknav creates tags on its own in order to work the way it does.

In that case my javascript looked like this.

$(document).ready(function(){
    $("#menuBlock div ul li a").click(function (e){
        e.preventDefault();
        var selected = $(this).attr('href');
        $( selected ).fadeIn('slow').siblings().hide();
        });
    });
Answer:1

There should a space between 2 selectors if they have a parent child relationship, so change this line

$('#main' + selected).show('slow').siblings().hide('slow');

to

$('#main ' + selected).show('slow').siblings().hide('slow');

or simply the selected one (since it is already pointing to a specific element)

$(selected).show('slow').siblings().hide('slow');

check this updated fiddle

$(document).ready(function(){
    $("#menu li a").click(function (e){ //bind the event on `a` rather than ul
        var selected = $(this).attr('href'); //use $(this) instead of this
        $( selected ).show('slow').siblings().hide('slow'); //explained above
    });
});
Answer:2

Try this method

$(document).ready(function() {
  $("#menu a").click(function(e) {
    debugger;
    var selected = $(this).attr('name');
    $('#main div').hide(); 
    $('#main div[name="'+selected+'"]').show('slow');
  });
});
Answer:3

I'm using the 500px API module with Node.js and I'm trying to get photos of a specific user. I'm facing issue with function, callback and scope... I've this code : api500px.photos.getByUsername ('...

I'm using the 500px API module with Node.js and I'm trying to get photos of a specific user. I'm facing issue with function, callback and scope... I've this code : api500px.photos.getByUsername ('...

  1. return data callback function
  2. return data callback
  3. return data callback javascript
  4. return data from callback node js
  5. return data from callback function javascript
  6. return data from callback js
  7. return data ajax callback
  8. nodejs return data callback
  9. node return data from callback
  10. python return data from callback
  11. matlab return data from callback

I have a php file on my server to upload file. I tried to upload using that file directly and it was success. Then I wrote a simple phantomjs script, run it on ubuntu with phantomjs version 2.1.1 but ...

I have a php file on my server to upload file. I tried to upload using that file directly and it was success. Then I wrote a simple phantomjs script, run it on ubuntu with phantomjs version 2.1.1 but ...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. cancun
  6. canada goose
  7. cane corso
  8. canes
  9. canadian prime minister
  10. cancun flights
  11. canker sore
  12. candy
  13. cancer
  14. canon
  15. canvas prints
  16. cancer sign
  17. candace owens
  18. canada news
  19. candytopia
  20. canlis

I have an AJAX setup in php to return and dispaly some data from DB: $("#sub_shops").click(function(){ var range_start = $("#start_shops").val(); var range_end ...

I have an AJAX setup in php to return and dispaly some data from DB: $("#sub_shops").click(function(){ var range_start = $("#start_shops").val(); var range_end ...

I'm trying to populate my site with ng-repeat images. I wanted to make something like if there is only one image in images, it's going to get 1 full row(col-xs-12), if there will be 2 images, they are ...

I'm trying to populate my site with ng-repeat images. I wanted to make something like if there is only one image in images, it's going to get 1 full row(col-xs-12), if there will be 2 images, they are ...