Show / hide text on click multiple IDs

jquery show / hide multiple div onclick example codepen
show/hide multiple divs javascript
jquery show/hide multiple divs independently
jquery show/hide multiple divs independently codepen
show/hide multiple divs javascript codepen
onclick show/hide div jquery demo codepen
show/hide multiple divs with select using jquery
onclick show div hide all others

I would like to show/hide toggle the text shown on a project I'm working on when I click on the title. i have managed to show the content when I click on the titles but I am not able to hide the content I do not need to display at the time. The idea is that if I click on title 1, the content in div id="content1" shows. When I click on title2, only div with id=content2 shows etc.

Thank you all in advance,

Jquery link: https://jsfiddle.net/dt32wshj/3/

   <div class="wrap">
   <h1 class="showcontent" id="1">Text 1</h1>
    <div class="content" id="content1">
      <h3 id="sub1">Sub 1.1</h3>
        <div class="sub" id="Xsub1"><p>Para 1.1</p></div>
      <h3 id="sub2">Sub 1.2</h3>
        <div class="sub" id="Xsub2"><p>Para 1.2</p></div>
    </div>
</div>

<div class="wrap">
  <h1 class="showcontent" id="2">Title 2</h1>
    <div class="content" id="content2">
      <h3 id="sub3">Sub 2.1</h3>
        <div class="sub" id="Xsub3"><p>Para 2.1</p></div>
      <h3 id="sub4">Sub 2.2</h3>
        <div class="sub" id="Xsub4"><p>Para 2.2</p></div>
    </div>
</div>
  <div class="wrap">
    <h1 class="showcontent" id="3">Title 3</h1>
  <div class="content" id="content3">
  <h3 id="sub5">Sub 3.1</h3>
  <div class="sub" id="Xsub5"><p>Para 3.1</p></div>
  <h3 id="sub6">Sub 3.2</h3>
  <div class="sub" id="Xsub6"><p>Para 3.2</p></div>
  </div>
</div>

JQuery

$(document).ready(function(){
            $(".showcontent").click(function () {
         var name = $(this).attr('id');
          $("#content" + name).fadeIn(1000);
            });    
                 $(".content h3").click(function () {
           var sub = $(this).attr('id');
          $("#X" + sub).slideDown(500);
            });

});

The idea is that before you show the current content, you must hide all other content

and you can do this without using ids especially if you have a large content, using classes is more appropriate

the same thing applies to the inner content

$(document).ready(function() {
  $(".showcontent").click(function() {
    $(".content").hide();
    $(this).next(".content").fadeIn(1000);
  });
  $(".content h3").click(function() {
     $(".sub").slideUp(500);
    $(this).next(".sub").slideDown(500);
  });

});
.sub {
  display: none;
}

.content {
  display: none;
}

h1 {
  cursor: pointer;
}

h1:hover {
  color: blue;
}

h3 {
  cursor: pointer;
}

h3:hover {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
  <h1 class="showcontent" id="1">Text 1</h1>
  <div class="content" id="content1">
    <h3 id="sub1">Sub 1.1</h3>
    <div class="sub" id="Xsub1">
      <p>Para 1.1</p>
    </div>
    <h3 id="sub2">Sub 1.2</h3>
    <div class="sub" id="Xsub2">
      <p>Para 1.2</p>
    </div>
  </div>
</div>

<div class="wrap">
  <h1 class="showcontent" id="2">Title 2</h1>
  <div class="content" id="content2">
    <h3 id="sub3">Sub 2.1</h3>
    <div class="sub" id="Xsub3">
      <p>Para 2.1</p>
    </div>
    <h3 id="sub4">Sub 2.2</h3>
    <div class="sub" id="Xsub4">
      <p>Para 2.2</p>
    </div>
  </div>
</div>
<div class="wrap">
  <h1 class="showcontent" id="3">Title 3</h1>
  <div class="content" id="content3">
    <h3 id="sub5">Sub 3.1</h3>
    <div class="sub" id="Xsub5">
      <p>Para 3.1</p>
    </div>
    <h3 id="sub6">Sub 3.2</h3>
    <div class="sub" id="Xsub6">
      <p>Para 3.2</p>
    </div>
  </div>
</div>

Show/hide function with multiple div ids, ready hide all its immediate children;; You could then target all a.close inside this container, and on click hide the clicked anchor's grand-  Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete

You can achieve this in only javascript by just hiding the relevant classes on click, then revealing just the one clicked:

(You can use .fadeout rather than .hide, depending on your preference)

$(document).ready(function() {

  $(".showcontent").click(function() {
    var name = $(this).attr('id');
    $(".content").hide();
    $("#content" + name).fadeIn(1000);
  });

  $(".content h3").click(function() {
    var sub = $(this).attr('id');
    $(".sub").hide();
    $("#X" + sub).slideDown(500);
  });

});

Advance jquery show/hide with multiple div - JavaScript, So right now I am coding a jqeury which show an hide specific divs. to make it when I click multiple times on the button there is a chance that two div boxes will the fiddle or the HTML I can only assume you have duplicate ID's in the HTML. When you click on the button, Show/Hide table, the table will hide if visible. If the table is not visible, upon clicking the button, the table will be visible. If the table is not visible, upon clicking the button, the table will be visible.

Plenty of ways you can achieve this. One is to save the currently open id in a var, and close it when it's time to open a different div:

$(document).ready(function(){

           //hide all content
          $("[id^=content]").hide();
          
          var currentShown = -1;

          $(".showcontent").click(function () {
           var name = $(this).attr('id');
           if (currentShown == name) return;
          //hide currently shown
          $("#content" + currentShown).fadeOut(1000);
          $("#content" + name).fadeIn(1000);
          currentShown = name;
         });
          
          
          $(".content h3").click(function () {
           var sub = $(this).attr('id');
          $("#X" + sub).slideDown(500);
            });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
   <h1 class="showcontent" id="1">Text 1</h1>
    <div class="content" id="content1">
      <h3 id="sub1">Sub 1.1</h3>
        <div class="sub" id="Xsub1"><p>Para 1.1</p></div>
      <h3 id="sub2">Sub 1.2</h3>
        <div class="sub" id="Xsub2"><p>Para 1.2</p></div>
    </div>
</div>

<div class="wrap">
  <h1 class="showcontent" id="2">Title 2</h1>
    <div class="content" id="content2">
      <h3 id="sub3">Sub 2.1</h3>
        <div class="sub" id="Xsub3"><p>Para 2.1</p></div>
      <h3 id="sub4">Sub 2.2</h3>
        <div class="sub" id="Xsub4"><p>Para 2.2</p></div>
    </div>
</div>
  <div class="wrap">
    <h1 class="showcontent" id="3">Title 3</h1>
  <div class="content" id="content3">
  <h3 id="sub5">Sub 3.1</h3>
  <div class="sub" id="Xsub5"><p>Para 3.1</p></div>
  <h3 id="sub6">Sub 3.2</h3>
  <div class="sub" id="Xsub6"><p>Para 3.2</p></div>
  </div>
</div>

The Best jQuery Examples, jQuery uses CSS-style selectors to select parts, or elements, of an HTML page. The jQuery .html() method gets the content of a HTML element or sets the content of To set multiple CSS properties, you'll have to use the object literal syntax like this: This code shows an alert when a user clicks a button: This is the "Quick 'N Easy Div Show/Hide" article. If you've been holding off on creating sections of your web page to show or hide on demand because you were uncertain exactly how to implement it, hold off no more. This is what I'm referring to: Click here to show/hide example text.

Hide and show multiple div with a single button click ?, I am New to JS I am trying Questions Page in javascript Like Multiple i check on next current div hide and next div(question) Show when click on back and previous div IDs and create two click events for previous and next. Hide or show elements in HTML using display property Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”.

jQuery Multiple Elements Selector, jQuery multiple elements Selector. ❮ jQuery Selectors. Example. Select all <h2>, <div> and <span> elements:. Today’s tutorial will show you how to hide away extra bits of content using CSS and JavaScript, to be revealed at the click of a button. This is a great technique, because displaying the additional content doesn’t require a refresh or navigation to a new page and all your content is still visible to search engine bots that don’t pay any

Calling JavaScript code on multiple DIV elements without ID attribute, In this tutorial, we'll take a look at how to call JavaScript code on multiple DIV Now whenever you click on any of the DIVs, you will have its object and  $(selector).show(speed,callback); The optional speed parameter specifies the speed of the hiding/showing, and can take the following values: "slow", "fast", or milliseconds. The optional callback parameter is a function to be executed after the hide() or show() method completes (you will learn more about callback functions in a later chapter).

Comments
  • Thanks :) this is what i needed