Show hide content when clicked on a specific id

2

Let's say I have two links

<a href="#pessoa1">link1</a>
<a href="#pessoa2">link2</a>

The content shown will only refer to the clicked link and when you click on another link the currently open content will be closed

<div id="pessoa1"></div>
<div id="pessoa2"></div>

<script>
$('a').click(function(e){
    $('[id^="pessoa"]').show("slow");
    e.preventDefault();
    return false;
});
</script>
    
asked by anonymous 02.07.2018 / 01:26

1 answer

2

This would be taking the href of the clicked element, which is id of its div :

$('a').click(function(e){
   $("div[id^='pessoa']").hide();
   var id = $(this).attr("href");
    $(id).show("slow");
    e.preventDefault();
});
div[id^='pessoa']{
   display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="#pessoa1">link1</a>
<a href="#pessoa2">link2</a>
<br>
<div id="pessoa1">p1</div>
<div id="pessoa2">p2</div>
  

return false; is not required.

    
02.07.2018 / 01:38