469,270 Members | 1,732 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Changing value of text fields not inside form

Why doesn't this work? :-

<BODY onkeydown="display(event)">
<input type="text" name="text1">
<script language="Javascript">
function display(event) {
document.text1.value = event.keyCode+' : '+String.fromCharCode(event.keyCode);
}
</script>
</BODY>

Error: document.text1 has no properties

I have to put it in a Form to get it to work :-

<BODY onkeydown="display(event)">
<form name="form1">
<input type="text" name="text1">
</form>
<script language="Javascript">
function display(event) {
document.form1.text1.value = event.keyCode+' : '+String.fromCharCode(event.keyCode);
}
</script>
</BODY>
Jul 23 '05 #1
1 1403
songkau wrote:
Why doesn't this work? :-

<BODY onkeydown="display(event)">
<input type="text" name="text1">
<script language="Javascript">
function display(event) {
document.text1.value = event.keyCode+' : '+String.fromCharCode(event.keyCode);
}
</script>
</BODY>

Error: document.text1 has no properties

I have to put it in a Form to get it to work :-

<BODY onkeydown="display(event)">
<form name="form1">
<input type="text" name="text1">
</form>
<script language="Javascript">
function display(event) {
document.form1.text1.value = event.keyCode+' : '+String.fromCharCode(event.keyCode);
}
</script>
</BODY>

It is OK to put form controls into an HTML page without making
them part of a form. However, if you do, you don't have access
to certain features that are available when they are part of a
form. For example, they can never be successful and so can't be
submitted as if they were in a form.

Have a read here:

<URL:http://jibbering.com/faq/faq_notes/form_access.html>
--
Rob
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by horos | last post: by
4 posts views Thread by Tony W | last post: by
2 posts views Thread by sheldonlg | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.