467,169 Members | 1,031 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

body - innerText.length after onkeydown

I'm facing a problem where if I press a key in the <body> element ,of a
dhtml page, the event is actioned before the actual key has been added to
the document.

In essence, I'm aiming to capture the length of Text entered and if > 0 then
do something. Can anyone please explain how i can ensure the document is
updated before I attempt to assess the length, otherwise I am out of sync?

I hope that makes sense!

Martin
Jul 23 '05 #1
  • viewed: 4686
Share:
4 Replies
VK
> I'm aiming to capture the length of Text entered

Text entered to where? Right to the body (over contenteditable) or to a
form control?

Jul 23 '05 #2


onkeydown triggers when the key is 'down', try instead with onkeyup,
which will trigger until the key is released UP or onkeypress which is
about the same.
Danny

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #3
ASM
Martin John Brindle wrote:
I'm facing a problem where if I press a key in the <body> element ,of a
dhtml page, the event is actioned before the actual key has been added to
the document.

In essence, I'm aiming to capture the length of Text entered and if > 0 then
do something. Can anyone please explain how i can ensure the document is
updated before I attempt to assess the length, otherwise I am out of sync?


<form>
<input type="text"
onkeydown="t=this.value"
onkeyup="if(t.length>10){
this.value=t;
alert('length allowed reached');
}">
</form>
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #4
Thanks guys. However, I've managed to hack this together and it works so I'm
happy! I take the length of the editable body and add the character provided
it's not a BACKSPACE.

<SCRIPT LANGUAGE="javascript">
<!--
var strIndex = "";

function input_onkeydown(docObj) {
var strEmpty = docObj.innerText + String.fromCharCode(event.keyCode);

if (window.event.keyCode != 8)
strEmpty.length == 0 ? strEmpty = "Y" : strEmpty = "N";
else
(strEmpty.length -1 == 0) || (strEmpty.length -2 == 0) ? strEmpty = "Y"
: strEmpty = "N";
. . .
}
-->
</SCRIPT>
<BODY id="input" onkeydown="input_onkeydown(this)">
<div name=foo contenteditable="true" align=left></div>
</BODY>
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by keerthana_m_2003@yahoo.com | last post: by
1 post views Thread by PCK | last post: by
5 posts views Thread by GarryJones | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.