473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Form element scope

Hi everyone,

I have a test page. When I press the button, it bring form.html,
which is fine.
When I press Enter key in the textbox, it gives me an error saying
this.form.cmd is not an object. But in the button, it used the code
this.form.cmd.value='dispense'. Why does it give me the error? I
think in a function, it should recognize the document, form and its
objects, should it?

Thank a lot.

Here is the page.
<form name="newRxForm" method="post" action="form.html">
<input type="hidden" name="cmd" value="dispense">

<script language='javascript1.2' >

function microsoftKeyPress(upc) {
if (window.event.keyCode == 13){
this.form.cmd.value="view";
f.submit();
}}

</script>
<input type="text" name="upc" value="upc"
onKeyPress="microsoftKeyPress(this.value)">
<input type="button" name="Submit3" value="Dispense" class="smallText"
onClick="this.form.cmd.value='dispense';this.form. submit()">
</form>
Jul 23 '05 #1
4 3925
st*********@yahoo.com (Steve) wrote in message news:<cd**************************@posting.google. com>...
Hi everyone,

I have a test page. When I press the button, it bring form.html,
which is fine.
When I press Enter key in the textbox, it gives me an error saying
this.form.cmd is not an object. But in the button, it used the code
this.form.cmd.value='dispense'. Why does it give me the error? I
think in a function, it should recognize the document, form and its
objects, should it?

Thank a lot.

Here is the page.
<form name="newRxForm" method="post" action="form.html">
<input type="hidden" name="cmd" value="dispense">

<script language='javascript1.2' >

function microsoftKeyPress(upc) {
if (window.event.keyCode == 13){
this.form.cmd.value="view";
f.submit();
}}

</script>
<input type="text" name="upc" value="upc"
onKeyPress="microsoftKeyPress(this.value)">
<input type="button" name="Submit3" value="Dispense" class="smallText"
onClick="this.form.cmd.value='dispense';this.form. submit()">
</form>


You've got two separate functions there: one (Button.onclick) is
anonymous, a property of the "Submit3" Button object, the other is
global (microsoftKeyPress) and is a property of window. Two different
scopes - 'this' in a top-level function points to window, in a form
event handler property, to the element object it's a property of.

You've also passed an argument (upc) for no reason, and referenced the
form as 'f' with no supporting code. Also effectively disabled the
form completely for JS-disabled users.
Jul 23 '05 #2
Thank you for the help. Could you give me more info?

I changed the code, but the line

this.document.form.cmd.value="view";

still caused problem.

The next line this.document.form.cmd.submit();

works. Why one worked, the other did not? You said "this" means
window object, didn't you?

In the code: onClick="this.form.cmd.value..., do you mean "this" refer
to the button? If this is what you mean, then "this.form.cmd.value"
does not make sense.

Here is the page.
<form name="newRxForm" method="post" action="form.html">
<input type="hidden" name="cmd" value="dispense">

<script language='javascript1.2' >

function microsoftKeyPress() {
if (window.event.keyCode == 13){
this.document.form.cmd.value="view";
this.document.form.cmd.submit();
}}

</script>
<input type="text" name="upc" value="upc"
onKeyPress="microsoftKeyPress()">
<input type="button" name="Submit3" value="Dispense"
onClick="this.form.cmd.value='dispense';this.form. submit()">
</form>
Jul 23 '05 #3
st*********@yahoo.com (Steve) wrote in message news:<cd**************************@posting.google. com>...
Thank you for the help. Could you give me more info?

I changed the code, but the line

this.document.form.cmd.value="view";

still caused problem.

The next line this.document.form.cmd.submit();

works. Why one worked, the other did not? You said "this" means
window object, didn't you?

In the code: onClick="this.form.cmd.value..., do you mean "this" refer
to the button? If this is what you mean, then "this.form.cmd.value"
does not make sense.

Here is the page.
<form name="newRxForm" method="post" action="form.html">
<input type="hidden" name="cmd" value="dispense">

<script language='javascript1.2' >

function microsoftKeyPress() {
if (window.event.keyCode == 13){
this.document.form.cmd.value="view";
this.document.form.cmd.submit();
}}

</script>
<input type="text" name="upc" value="upc"
onKeyPress="microsoftKeyPress()">
<input type="button" name="Submit3" value="Dispense"
onClick="this.form.cmd.value='dispense';this.form. submit()">
</form>
You still haven't explained what you needed to do.

Never a good idea to make a form impossible to submit w/o JavaScript.

Try this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>

<script type="text/javascript">
//<![CDATA[

function handle_keypress(e)
{
e = e || window.event; //get Event object
var kC = e.keyCode || e.which; //key code
if (e && kC && kC == 13) //enter key?
{
var tgt = e.srcElement || e.target; //'upc' field
if (tgt)
{
var f = tgt.form; //Form object
f.cmd.value = 'view'; //set flag
}
}
return true; //why not...
}

function handle_enter()
{
var el;
if (el = document.getElementById('upc'))
el.onkeypress = handle_keypress;
}

onload = handle_enter;

//]]>
</script>
</head>
<body>
<form name="newRxForm" method="post" action="form.html">
<input type="hidden" name="cmd" value="dispense">
<input id="upc" type="text" name="upc" value="upc">
<input type="submit" name="Submit3" value="Dispense" class="smallText"
onClick="this.form.cmd.value='dispense'">
</form>
</body>
</html>

Keep this in mind: embedding a script in the middle of a form doesn't
make it a part of the form. Different browsers will modify the scope
of varibles in such a script, but it's nothing to rely on. Always
treat them like separate entities.
In the code: onClick="this.form.cmd.value..., do you mean "this" refer
to the button? If this is what you mean, then "this.form.cmd.value"
does not make sense.


Sure it does.

this == Button
..form == form property, points to Form object (every form element has
one)
..cmd == hidden field of that name in form
..value == its value property

'this' in that other (stand-alone) function points to 'window', and a
window object has no .form property - what would it refer to if it
did? ;)
Jul 23 '05 #4
fe******@hotmail.com (RobB) wrote in message news:<ab**************************@posting.google. com>...
st*********@yahoo.com (Steve) wrote in message news:<cd**************************@posting.google. com>...
Thank you for the help. Could you give me more info?

I changed the code, but the line

this.document.form.cmd.value="view";

still caused problem.

The next line this.document.form.cmd.submit();

works. Why one worked, the other did not? You said "this" means
window object, didn't you?

In the code: onClick="this.form.cmd.value..., do you mean "this" refer
to the button? If this is what you mean, then "this.form.cmd.value"
does not make sense.

Here is the page.
<form name="newRxForm" method="post" action="form.html">
<input type="hidden" name="cmd" value="dispense">

<script language='javascript1.2' >

function microsoftKeyPress() {
if (window.event.keyCode == 13){
this.document.form.cmd.value="view";
this.document.form.cmd.submit();
}}

</script>
<input type="text" name="upc" value="upc"
onKeyPress="microsoftKeyPress()">
<input type="button" name="Submit3" value="Dispense"
onClick="this.form.cmd.value='dispense';this.form. submit()">
</form>


You still haven't explained what you needed to do.

Never a good idea to make a form impossible to submit w/o JavaScript.

Try this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>

<script type="text/javascript">
//<![CDATA[

function handle_keypress(e)
{
e = e || window.event; //get Event object
var kC = e.keyCode || e.which; //key code
if (e && kC && kC == 13) //enter key?
{
var tgt = e.srcElement || e.target; //'upc' field
if (tgt)
{
var f = tgt.form; //Form object
f.cmd.value = 'view'; //set flag
}
}
return true; //why not...
}

function handle_enter()
{
var el;
if (el = document.getElementById('upc'))
el.onkeypress = handle_keypress;
}

onload = handle_enter;

//]]>
</script>
</head>
<body>
<form name="newRxForm" method="post" action="form.html">
<input type="hidden" name="cmd" value="dispense">
<input id="upc" type="text" name="upc" value="upc">
<input type="submit" name="Submit3" value="Dispense" class="smallText"
onClick="this.form.cmd.value='dispense'">
</form>
</body>
</html>

Keep this in mind: embedding a script in the middle of a form doesn't
make it a part of the form. Different browsers will modify the scope
of varibles in such a script, but it's nothing to rely on. Always
treat them like separate entities.
In the code: onClick="this.form.cmd.value..., do you mean "this" refer
to the button? If this is what you mean, then "this.form.cmd.value"
does not make sense.


Sure it does.

this == Button
.form == form property, points to Form object (every form element has
one)
.cmd == hidden field of that name in form
.value == its value property

'this' in that other (stand-alone) function points to 'window', and a
window object has no .form property - what would it refer to if it
did? ;)


Oops...don't forget to remove this from the submit button, else moz
will run it even when the enter key is used:

onClick="this.form.cmd.value='dispense'"
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

72
by: Stephen Poley | last post by:
I have quite often (as have probably many of you) come across HTML forms with irritating bits of Javascript attached. The last straw on this particular camel's back was a large form I was asked to...
12
by: Jim Tome | last post by:
Hi, I am trying to change and pass the value of a hidden input type on a form tag to a cgi processing script based on the value of a checkbox within the form: function CheckBoxes () { if...
4
by: Stuart Perryman | last post by:
Hi, I have the following code which works just fine in IE6 but not in Firefox. It is an extract of several table rows each with an individual form. It is generated by php. <form...
4
by: Roshawn Dawson | last post by:
Hi, I'm having the hardest time trying to understand the proper usage of forms in ASP.NET. Alrightie then! I have a form on my ASP.NET page. To be more specific, this page displays the...
2
by: Roshawn Dawson | last post by:
Hi, I seem to be having trouble using the Request.Form property. Before explaining what I'm trying to do, have a look at my html: <form id="Cart" method="post" action="ShoppingCart.aspx">...
3
by: namewitheldbyrequest | last post by:
"The XML element 'EnableTheming' from namespace 'http://tempuri.org/' is already present in the current scope" I created a Web Service: I imported System.Data.SqlClient so I could access SQL...
6
by: drec | last post by:
I am just learning Javascript and I would like to create a basic form that gives me two options. This will be using either checkbox or radio input type, however I would like the second option to...
4
by: jedimasta | last post by:
Good evening all, I'm a relatively new to javascript, but I've been working with ColdFusion and PHP for years so I'm not necessarily ignorant, just stuck and frustrated. Using ColdFusion I'm...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.