Search code examples
javascriptjquerybulma

Simple jQuery toggle with bulma card not working


I have a little bulma card. I tried to display it, when I click on the icon:

  <div class="card">
    <header class="card-header">
      <p class="card-header-title">
        Component
      </p>
      <a href="#" class="card-header-icon" aria-label="more options" id="server">
        <span class="icon">
          <i class="fas fa-angle-down" aria-hidden="true"></i>
        </span>
      </a>
    </header>
    <div class="card-content is-hidden" id="server-content">
      <div class="content">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec iaculis mauris.
        <a href="#">@bulmaio</a>. <a href="#">#css</a> <a href="#">#responsive</a>
        <br>
        <time datetime="2016-1-1">11:09 PM - 1 Jan 2016</time>
      </div>
    </div>
  </div>

This I tried:

$(document).ready(function() {

  $("#server").click(function() {
    $("#server-content").toggle();
  });
});

But it didn't work. In my HTML Head I do this:

  <script src="js/jquery.js" type="text/javascript"></script>
  <script src="js/main.js" type="text/javascript"></script>

In the main.js file is the snippet from above. When I put an alert inside the jquery function it will be pop up, but the content is still hidden.


Solution

  • You just need to use jQuery's toggleClass function instead of toggle. is-hidden class in bulma will hide the div it is applied to. Please check at https://jsfiddle.net/rutsxz91/3/

    $(document).ready(function() {
    
      $("#server").click(function() {
        $("#server-content").toggleClass('is-hidden');
      });
    });