473,804 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.v alue='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.ht ml">
<input type="hidden" name="cmd" value="dispense ">

<script language='javas cript1.2' >

function microsoftKeyPre ss(upc) {
if (window.event.k eyCode == 13){
this.form.cmd.v alue="view";
f.submit();
}}

</script>
<input type="text" name="upc" value="upc"
onKeyPress="mic rosoftKeyPress( this.value)">
<input type="button" name="Submit3" value="Dispense " class="smallTex t"
onClick="this.f orm.cmd.value=' dispense';this. form.submit()">
</form>
Jul 23 '05 #1
4 3951
st*********@yah oo.com (Steve) wrote in message news:<cd******* *************** ****@posting.go ogle.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.v alue='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.ht ml">
<input type="hidden" name="cmd" value="dispense ">

<script language='javas cript1.2' >

function microsoftKeyPre ss(upc) {
if (window.event.k eyCode == 13){
this.form.cmd.v alue="view";
f.submit();
}}

</script>
<input type="text" name="upc" value="upc"
onKeyPress="mic rosoftKeyPress( this.value)">
<input type="button" name="Submit3" value="Dispense " class="smallTex t"
onClick="this.f orm.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 (microsoftKeyPr ess) 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.f orm.cmd.value=" view";

still caused problem.

The next line this.document.f orm.cmd.submit( );

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

In the code: onClick="this.f orm.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.ht ml">
<input type="hidden" name="cmd" value="dispense ">

<script language='javas cript1.2' >

function microsoftKeyPre ss() {
if (window.event.k eyCode == 13){
this.document.f orm.cmd.value=" view";
this.document.f orm.cmd.submit( );
}}

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

I changed the code, but the line

this.document.f orm.cmd.value=" view";

still caused problem.

The next line this.document.f orm.cmd.submit( );

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

In the code: onClick="this.f orm.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.ht ml">
<input type="hidden" name="cmd" value="dispense ">

<script language='javas cript1.2' >

function microsoftKeyPre ss() {
if (window.event.k eyCode == 13){
this.document.f orm.cmd.value=" view";
this.document.f orm.cmd.submit( );
}}

</script>
<input type="text" name="upc" value="upc"
onKeyPress="mic rosoftKeyPress( )">
<input type="button" name="Submit3" value="Dispense "
onClick="this.f orm.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.dt d">
<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.getEle mentById('upc') )
el.onkeypress = handle_keypress ;
}

onload = handle_enter;

//]]>
</script>
</head>
<body>
<form name="newRxForm " method="post" action="form.ht ml">
<input type="hidden" name="cmd" value="dispense ">
<input id="upc" type="text" name="upc" value="upc">
<input type="submit" name="Submit3" value="Dispense " class="smallTex t"
onClick="this.f orm.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.f orm.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******@hotmai l.com (RobB) wrote in message news:<ab******* *************** ****@posting.go ogle.com>...
st*********@yah oo.com (Steve) wrote in message news:<cd******* *************** ****@posting.go ogle.com>...
Thank you for the help. Could you give me more info?

I changed the code, but the line

this.document.f orm.cmd.value=" view";

still caused problem.

The next line this.document.f orm.cmd.submit( );

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

In the code: onClick="this.f orm.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.ht ml">
<input type="hidden" name="cmd" value="dispense ">

<script language='javas cript1.2' >

function microsoftKeyPre ss() {
if (window.event.k eyCode == 13){
this.document.f orm.cmd.value=" view";
this.document.f orm.cmd.submit( );
}}

</script>
<input type="text" name="upc" value="upc"
onKeyPress="mic rosoftKeyPress( )">
<input type="button" name="Submit3" value="Dispense "
onClick="this.f orm.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.dt d">
<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.getEle mentById('upc') )
el.onkeypress = handle_keypress ;
}

onload = handle_enter;

//]]>
</script>
</head>
<body>
<form name="newRxForm " method="post" action="form.ht ml">
<input type="hidden" name="cmd" value="dispense ">
<input id="upc" type="text" name="upc" value="upc">
<input type="submit" name="Submit3" value="Dispense " class="smallTex t"
onClick="this.f orm.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.f orm.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.f orm.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
5246
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 complete in connection with attendance at a seminar. After spending more than 15 minutes on it, I clicked on the submit button - and nothing happened. Looking round the pages on Javascript form validation that Google produced for me (well,...
12
2172
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 (document.MyForm.CheckBox1.checked) { document.MyForm.recipient.value = "2"; document.MyForm.submit();
4
9306
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 action="MaintNotification.php?ReqID=5" method="post" name="frm5"> <tr align="left" bgcolor="#dddddd" class="text" onClick="submit()"
4
2040
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 items in one's shopping cart. Its layout is done using a Repeater control. With the exception of the Repeater control and a button, all other controls inside the form are not server controls (none contain the runat="server" attribute). They are...
2
2841
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"> <table id="items" cellpadding="0" cellspacing="0"> <caption>Your Shopping Cart</caption> <thead> <tr>
3
10843
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 server tables programmatically. The web service builds and deploys with no problems. When I try to add the corresponding Web Reference to the Web Site project I get the error listed below. I am able to create other Web Services on the same server...
6
1914
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 allow the user to type in a value. Also, I would like the 2nd option only editable if the button for that option is selected. All I can seem to find is basic examples of forms, and none of which have this feature. The form would look something...
4
7270
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 using an include to pull in form elements (text fields, checkboxes, etc...) multiple times on a single page. The included page does not have a form tag of it's own, but the root page has uniquely named forms for validation. Imagine it like this:
2
2661
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
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.