Hide div when you click it

Asked

Viewed 152 times

0

How can I click the div and stop displaying it? I’m using onClick but it’s not running.

My div:

<div class='alert alert-danger aviso' role='alert' style='width: 20%;'>Falha ao carregar os dados!</div>

onClick:

$('.aviso').on("click", function () {
                    $('.aviso').hide();
                }
  • Is there an error in the console? And please elaborate a [mcve] of your problem using the site’s snippet (button </> of the editor).

  • 1

    Cannot hide pq missing closing function with the ) after the }. Probably if you open the console (F12) see the error printed on it.

2 answers

1


If you have entered your complete code, you are missing close the click assignment, missing one ); in the end. I believe that’s because I put your code in a fiddle and it worked, and here in the snippet as well:

$('.aviso').on("click", function () {
  $('.aviso').hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class='alert alert-danger aviso' role='alert' style='width: 20%;'>Falha ao carregar os dados!</div>

In fact, to ensure that it will only close the div that was clicked, I suggest replacing the line $('.aviso').hide(); for $(this).hide();

0

Browser other questions tagged

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