0

I have five button which contain different data

$(".Datanewpost").click(function() {
  $(".data).toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data">
  <div id="Datanewpost">
    Data 1
    <div class="data"> Helol</div>
  </diV>
  <div id="Datanewpost">
    data 2
    <div class="data"> Helol</div>
  </div>
  <div id="Datanewpost">
    data 3
    <div class="data"> Helol</div>
  </div>
  <div id="Datanewpost">
    data 5
    <div class="data"> Helol</div>
  </div>
  <div id="Datanewpost">
    data 5
    <div class="data"> Helol</div>
  </div>
</div>

Unfortunately this does not work what do I need to do to get what I want? I know I can do it by assigning individual id to each div but that looks bad coding

NOTE: I want to be able to hide or show individually ,

3
  • 3
    Your divs all have the same id. ids have to be unique. And the selector you use looks at the class Datanewpost , where no element has that class. So instead of using the same id for every post, use the same class. Commented Sep 19, 2018 at 14:41
  • 3
    syntax error in $(".data).toggle; should be $(".data").toggle() Commented Sep 19, 2018 at 14:46
  • 1
    You have two very obvious issues. Firstly you're missing the closing " in the selector in the jQuery object and the repeated id needs to be changed to a class. Commented Sep 19, 2018 at 14:52

3 Answers 3

1

Id should be unique in a document, use class instead. You can use $(this).find('.data') to toggle the specific element:

$(".Datanewpost").click(function(){
  $(this).find('.data').toggle();
});
.data{color: red}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="data-main">
   <div class="Datanewpost">
      Data 1
      <div class="data">  Hello</div>
   </diV>
   <div class="Datanewpost">
      Data 2
      <div class="data">  Hello</div>
   </div>
   <div class="Datanewpost">
      Data 3
      <div class="data">  Hello</div>
   </div>
   <div class="Datanewpost">
      Data 4
      <div class="data">  Hello</div>
   </div>
   <div class="Datanewpost">
      Data 5
      <div class="data">  Hello</div>
   </div>
</div>

As in comment, you have asked to close the open div when clicking another, simply loop through all the .data except the current one. Then check display='block' property to set that to display='none':

$(".Datanewpost").click(function(){
  $(this).find('.data').toggle();
  var current = $(this).find('.data');
  $('.data').not(current).each(function(i, el){
    if(this.style.display=='block')
      this.style.display = 'none';
  });
});
.data{
  color: red;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="data-main">
   <div class="Datanewpost">
      Data 1
      <div class="data">  Hello</div>
   </diV>
   <div class="Datanewpost">
      Data 2
      <div class="data">  Hello</div>
   </div>
   <div class="Datanewpost">
      Data 3
      <div class="data">  Hello</div>
   </div>
   <div class="Datanewpost">
      Data 4
      <div class="data">  Hello</div>
   </div>
   <div class="Datanewpost">
      Data 5
      <div class="data">  Hello</div>
   </div>
</div>

Sign up to request clarification or add additional context in comments.

11 Comments

I want to be able to hide or show individual , like when u click one button does not affext other button ,
@user9964622, current solution is doing the exact. When you click Data1, it only hide, show the next hello to it...........same goes for all........thanks
I tried but it hide all helo check here jsfiddle.net/yjs7nbov/2
@user9964622, there is something wrong with the HTML, I have used from my answer and is working nicely......thanks
@user9964622, I have got the issue of why the code in the fiddle not working as expected.....you have one </diV> (capital V). Should be </div>
|
1

When you have multiple same elements, you should use a class instead of id like the following:

$(".Datanewpost").click(function () {
  $(this).find(".data").toggle();
});
.Datanewpost {
  padding: 10px;
  background-color: #f7f7f7;
  margin: 5px;
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="data">
  <div class="Datanewpost">
    Data 1
    <div class="data"> Helol</div>
  </diV>
  <div class="Datanewpost">
    data 2
    <div class="data"> Helol</div>
  </div>
  <div class="Datanewpost">
    data 3
    <div class="data"> Helol</div>
  </div>
  <div class="Datanewpost">
    data 5
    <div class="data"> Helol</div>
  </div>
  <div class="Datanewpost">
    data 5
    <div class="data"> Helol</div>
  </div>
</div>

2 Comments

Hii , does this hide each element indiviadual ?
@user9964622 hi, yes
1

You can use like this:

$(".Datanewpost").click(function() {
  $(this).find(".data").toggle();

});

2 Comments

will this hide indivudual ? when u click first button it should hide or show only its data not all
Yes will this hide individual.

Your Answer

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.