slideToggle(); for row in tables

Asked

Viewed 53 times

0

$(document).ready(function(){
    $("#flip").click(function(){
        $("#panel").slideToggle("slow");
    });
});



<div id="flip"><table><td>Maria</td></table></div>
<div id="panel"><table><td>1.000</td></table></div>

<div id="flip"><table><td>José</td></table></div>
<div id="panel"><table><td>2.000</td></table></div>

Following the code above, I need to click on Mary and appear the value below (1,000), and the same with Joseph. It is working perfectly when you click on Maria, but not for José. How do I solve this problem?

  • You are using id which is a unique identifier and only the first element found in the document is selected, try switching to class, example <div class="flip"></div> and $('.flip') related https://answall.com/questions/39875/qual-a-prioridade-do-html-id-ou-class/39879#39879

  • The difference is that my original, it hides the line below and only when clicked, it appears, and its edition does the opposite.

  • Look at the answer, if that’s what you want, see that I’ve added a $(this).next('.panel') to select the next element, i.e.

1 answer

1


You are using id which is a unique identifier and only the first element found in the document is selected, try switching to class, example:

$(document).ready(function(){
    $(".flip").click(function(){
        $(this).next(".panel").slideToggle("slow");
    });
});
.flip{
  background-color:red;
  cursor:pointer;
}
.panel{
  background-color:blue;
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="flip"><table><td>Maria</td></table></div>
<div class="panel"><table><td>1.000</td></table></div>

<div class="flip"><table><td>José</td></table></div>
<div class="panel"><table><td>2.000</td></table></div>

  • PERFECT. It worked. I would like to know, how to leave the line with the hidden numbers when you load the page and only appear if clicked. The reverse path of your change. It has as?

  • @Rafaelbrito has yes, just add the property display:none in class .panel of the CSS.

  • 100% NOW. thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.