PDA

View Full Version : inline setTimeout function problems...



jussa
08-27-2007, 06:01 AM
hey all,

I have the following code in my 'onkeyup' event, which works fine...


<input type="text" id="txtActivityCode" class="textbox" style="width:211px" onkeyup="setTimeout(displayActivityCodes(this.value), 1000)" />

however, when i change the onkeyup event to be like so:

<input type="text" id="txtActivityCode" class="textbox" style="width:211px" onkeyup="setTimeout('displayActivityCodes(this.value)', 1000)" />
which is what is in all the examples i have seen, and also in the firefox error console (useless setTimeout call (missing quotes around argument?)), it wont send the value of this.value...instead it becomes undefined...

how do i get around this?

Cheers,
Justin

glenngv
08-27-2007, 06:16 AM
It doesn't work because setTimeout works in global scope. Meaning, this refers to the global window object not to the HTML control object.

Change your code to:

<script type="text/javascript">
function displayCodes(code){
setTimeout(function(){displayActivityCodes(code)}, 1000);
}
</script>
<input type="text" id="txtActivityCode" class="textbox" style="width:211px" onkeyup="displayCodes(this.value);" />

jussa
08-27-2007, 07:08 AM
Hi,

Thanks for your reply...that worked

im finding im needing more and more functions..so its all abit mind boggling trying to retrace. lol

Thanks again,
Justin