How to disable a button after a single click with validation using javascript?

First, I want to check some of the entered field values ​​or not. After that, When I click the submit button, the button should be disabled after one click to avoid duplicate sending. How to do this using javascript?

<script type="text/javascript" language="javascript"> function ValidateIt() { if (document.getElementById('ddlProblemCategory').value == 0) { alert("Please fill some value"); return false; } return true; } function DisableIt() { if (ValidateIt() == true) document.getElementById('btnSaveProblem').disabled = true; } </script> 

+6
source share
3 answers

You can add an onclick handler to your button:

 document.getElementById("idOfButton").onclick = function() { //disable this.disabled = true; //do some validation stuff } 
+10
source

Function call submitbtn onclick buttons.

Use

 function submitbtn(){ getElementById("Submit_id").disabled=true; //Validation code goes here } 
+2
source

Use hidden label and change its value to 1st click

 <script type = "text/javascript" language = "javascript"> function disableButton() { var lblText = document.getElementById('lbl_hdn_text').innerHTML; if (lblText == "true") { document.getElementById('lbl_hdn_text').innerHTML = "false"; return true; } else { return false; } } </script> <label id="lbl_hdn_text" style = "display:none;" >true</label> 
0
source

Source: https://habr.com/ru/post/947330/


All Articles