How not to let the user click more than once on javascript button

Asked

Viewed 134 times

-2

How not to let the user click more than once on javascript button

<input type="submit" class="inviar" name="teste" id="testeid" value="Enviar">
  • 1

    Welcome to the website. But you should put everything here in this question post, the stack is not a forum, You should always add information in the first post (the question). I suggest you edit it by clicking EDIT and delete the answers below. Visit [tour] to know the site better too :)

  • Another thing, always post the code in textual form, image only disturbs testing.

  • 1

    It depends. Is that inside a form? If it is a common form, without ajax, each click will reload the page and whether it has already been clicked or not it would need to be solved on the server side. See if any of the answers below solves your doubt, otherwise explain the problem better.

3 answers

4

Just add the disabled to disable the button, so "find a way" to add the attribute when clicked.

One of the options is to simply add in onClick:

<button onClick="this.disabled=true;">Teste</button>

You can also use Javascript, there is no need to use Jquery:

document.getElementsByTagName("button")[0].addEventListener('click', function(){
     this.disabled = true;
});
<button>Teste</button>

Remembering that the user can remove this behavior, as well as replicate the requests in other ways.

1


In jquery Voce you can do so:

$(document).ready(function () {
     $("#send").one('click', function (e) {  
           $(this).prop('disabled', true);
     });
});

0

What you can do is by clicking the button, change the 'disabled' property of the button to 'true':

Example:

Document.getElementById("myBtn"). disabled = true;

Source: Button disabled Property

Browser other questions tagged

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