Dimi Dimi - 12 days ago 7
Javascript Question

Disable Cancel button before POST

On clicking

Submit
button my JavaScript code execute
$.post("Submit.aspx",...
that sometimes takes a few seconds so user is able to click
Back
button that is undesirable.

So I try to disable or hide Back button but it does not work and it is still clickable. Any clue?

<input type="submit" value="Submit" class="next" onclick="submitData()" />
<input type="submit" id="btnBack" value="Back" onclick="goBack()" />

function submitData()
{
// I try to disable or hide Back button but it does not work
$('#btnBack').hide();
$('#btnBack').attr("disabled",true);
var btnBack= document.getElementById("btnBack").disabled = true;


// Long request
$.post("Submit.aspx",..
}

Answer

Your code seems to be right. Did you add Jquery?

Check the this:

function submitData(){
   // I try to disable or hide Back button but it does not work 
  $('#btnBack').hide();
  $('#btnBack').attr("disabled",true);
  var btnBack= document.getElementById("btnBack").disabled = true;

}

function goBack(){
  console.log("I'm back");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" value="Submit" class="next" onclick="submitData()" />
<input type="submit" id="btnBack" value="Back" onclick="goBack()" />