Can anyone tell me how to disable the submit button after it has been pressed 20 times? The HTML
runs like this: <input type=submit name='sub' value='submit' onClick='this.disabled=true;return true;doSection(loading);'>
and this code <p><input type="submit" name="submit" value="Submit" disabled /></p>
What should I do so that after pressing the button 20 times, it is dsabled?
You can have a counter that gets updated with every click on the button, when the counter has reached 20, disable it:
count = 0;
button = document.getElementsByTagName("input")[0]
/*assuming it's the only submit button in the page */
button.onclick = function() {
count++;
if (count == 20) {
this.disabled = true;
}
};
<!DOCTYPE html>
<html>
<body>
<p>Click the button to trigger a function.</p>
<button id="demo">Click me</button>
</body>
<script>
var nbSubmits = 0;
document.getElementById("demo").onclick = function (){
nbSubmits++;
if(nbSubmits == 20) {
document.getElementById("demo").disabled="true";
}
}
</script>
</html>
If you do a return true;doSection(loading);
, I suspect doSection will never get executed. Put the return true;
in last position.
Also please note that a submit button is supposed to submit the fields of its parent form. So you should be redirected to another page (or the same). Which means if you use javascript to count the click number, the counter will get reset after the first submit.
<script>
var NumberOfClicks = 0;
function disbale(thisobject)
{
NumberOfClicks++;
if(NumberOfClicks == 2){
thisobject.disabled="true";
}
}
</script>
<button id="demo" onclick="disbale(this)">Click me</button>