Forum Moderators: open

Message Too Old, No Replies

Disable an input briefly, then restore it

Trouble with setTimeout...

         

whoisgregg

8:25 pm on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I really expected this to just work:
<html><head>
<script type="text/javascript">
<!--
function disable(el,time){
var elid = el.id;
el.disabled = true;
setTimeout( "undisable("+elid+")" , time*1000);
}
function undisable(elid){
alert('Undisabling: '+elid);
var el = document.getElementById(elid);
el.disabled = false;
}
//-->
</script>
</head><body>
<input type="button" onclick="disable(this,2);" value="Click and I will briefly become unavailable" />
</body></html>

The code successfully disables the input but does not enable the input when the settimeout fires. Instead "elid" reports as "undefined"

I'm in the weird position of having buttons I need to prevent the possibility of a double-click, but allow later clicks. :/

I already have a CSS display toggle (none<=>block) but the button disappearing is just silly looking.

DrDoc

4:41 am on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you can't use the
id
as a reference when your submit button has none ;)

DrDoc

4:45 am on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<html><head>
<script type="text/javascript">
<!--
function disable(el,time){
var elid = el.id;
el.disabled = true;
setTimeout( "undisable('"+elid+"')" , time*1000);
}
function undisable(elid){
alert('Undisabling: '+elid);
var el = document.getElementById(elid);
el.disabled = false;
}
//-->
</script>
</head><body>
<input type="button" onclick="disable(this,2);" value="Click and I will briefly become unavailable" id="foo" />
</body></html>

whoisgregg

12:40 pm on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you can't use the id as a reference when your submit button has none ;)

::slaps forehead:: Thanks Doc! :)