Hide button with jQuery

Asked

Viewed 942 times

-2

I would like to know how to hide a button that only the logged in admin can see. I have a business rule with the same interface, but it has a specific button that only the admin has access to. How to do this with jquery?

2 answers

0

You are probably receiving the information through your serving language, knowing this, I recommend that you use your own if, checking that your user is an administrator, if he is not, you simply do nothing.

For example in PHP:

<?php if(usuario.permissao == "administrador"){ ?>
<button></button>
<?php } ?>

If this information is coming through a Javascript request, for example, Ajax. You can do the same check on JS, but you should capture the element and hide it.

Example:

if(data.permissao !== "administrador"){
var btn = $("#idDoSeuBtn");
btn.hide();
}

I hope to have helped you, if there are still doubts, please let me know how your information arrives to be made the corrections.

-1


You can use Jquery’s Hide anointing. See below:

$(document).ready(function(){
  if(true /* sua validação se é admin aqui*/){
    $('#btn-salvar').hide()
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<button  id="btn-visualizar"> Visualizar </button>
<button id="btn-salvar"> Salvar </button>

  • Thanks my friend thanks

  • 2

    Hiding a button that only an administrator can use in the frontend is a security flaw, this button should be hidden in the server. Otherwise any user who knows how to inspect element can show the button and click on it, even if not administrator.

  • Yes thanks for the help I’m doing test you know to see how I do to hide the button, thanks for the help

Browser other questions tagged

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