473,785 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

js onclick event to read values from form fails - why?


Whats wrong with my sample script below? All I want to do is confirm that
my form data is in my environment (basically, I'm writing a script that will
check that some form fields have a value, and others don't - The
functionality of this will be used in my other script but I want to make
sure I know whats happening first as opposed to bastardizing someone else's
work).

I have worked some of the code out - I figured the what should be working
below would be easy enough but again, I'm coming up with a surprise... I'm
not getting any errors... "firstFunction( )" manages to get the first
document.write to work, but the rest of it fails (ie no values, no errors).

Can someone help?

Much appreciated,
randelld

ps here's my code...

<script language="JavaS cript">
function firstFunction()
{
document.write( "<br>document.p atient.length = " + document.patien t.length);
document.write( "<br>document.p atient.weeks.va lue = " +
document.patien t.weeks.value);
document.write( "<br>document.p atient.elements[1].value = " +
document.patien t.elements[1].value);
return;
}
</script>

<form name="patient">
<p></p><hr>
Insanity checklist. Enter number of weeks: <input name="weeks" type="text">

<p></p>
Clothing preferences:
<select name="diet">
<option value="napolean ">Dr Napolean</option>
<option value="darcy">D r Darcy</option>
<option value="strangel ove">Dr Strangelove</option>
</select>
<br>
<input type="submit" onclick="firstF unction(); return">
<hr><a href="./e.html">again</a><hr>
</form>
Jul 20 '05 #1
7 2990
Randell D. wrote:

Whats wrong with my sample script below? All I want to do is confirm that
my form data is in my environment (basically, I'm writing a script that
will check that some form fields have a value, and others don't - The
functionality of this will be used in my other script but I want to make
sure I know whats happening first as opposed to bastardizing someone
else's work).

I have worked some of the code out - I figured the what should be working
below would be easy enough but again, I'm coming up with a surprise... I'm
not getting any errors... "firstFunction( )" manages to get the first
document.write to work, but the rest of it fails (ie no values, no
errors).

Can someone help?

Much appreciated,
randelld

ps here's my code...

<script language="JavaS cript">
function firstFunction()
{
document.write( "<br>document.p atient.length = " +
document.patien t.length);
document.write( "<br>document.p atient.weeks.va lue = " +
document.patien t.weeks.value);
document.write( "<br>document.p atient.elements[1].value = " +
document.patien t.elements[1].value);
return;
}
</script>

Stop right there. :-)

Why are you writing <br>document.et c.etc here?
DO you expect the browser to understand that?
I think you mean \n (NEWLINE) instead of the HTML-entity <br> IF you want to
generate dynamical javascript. If that is what you are trying, you do it
the wrong way, sorry.

But more important: WHY are you dynamically writing Javascript instead of
just checking the form the normal way?

If you want your function to return some values, use alert, and NOT
document.write.
So:
function firstFunction()
{
alert("document .patient.length = " + document.patien t.length);
alert("document .patient.weeks. value = " + document.patien t.weeks.value);
alert("document .patient.elemen ts[1].value = " +
document.patien t.elements[1].value);
}
Good luck and stop writing to your document, it is unneeded complex and
incorrect in this case. :-)

Regards,
Erwin Moller

<form name="patient">
<p></p><hr>
Insanity checklist. Enter number of weeks: <input name="weeks"
type="text">

<p></p>
Clothing preferences:
<select name="diet">
<option value="napolean ">Dr Napolean</option>
<option value="darcy">D r Darcy</option>
<option value="strangel ove">Dr Strangelove</option>
</select>
<br>
<input type="submit" onclick="firstF unction(); return">
<hr><a href="./e.html">again</a><hr>
</form>


Jul 20 '05 #2
DU
Randell D. wrote:
Whats wrong with my sample script below? All I want to do is confirm that
my form data is in my environment (basically, I'm writing a script that will
check that some form fields have a value, and others don't - The
functionality of this will be used in my other script but I want to make
sure I know whats happening first as opposed to bastardizing someone else's
work).

That's understandable.
I have worked some of the code out - I figured the what should be working
below would be easy enough but again, I'm coming up with a surprise... I'm
not getting any errors... "firstFunction( )" manages to get the first
document.write to work, but the rest of it fails (ie no values, no errors).

Can someone help?

Much appreciated,
randelld

ps here's my code...

<script language="JavaS cript">
Make sure you're using valid and validated markup code. Here, language
is deprecated; type has superseded language and is both backward and
forward-compatible. So

<script type="text/javascript">

function firstFunction()
{
document.open() ;
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-72161170
document.write( "<br>document.p atient.length = " + document.patien t.length);
document.write( "\ndocument.pat ient.length = " + document.patien t.length);
document.write( "<br>document.p atient.weeks.va lue = " +
document.patien t.weeks.value);
document.write( "\ndocument.pat ient.weeks.valu e = " +
document.patien t.weeks.value);

document.write( "<br>document.p atient.elements[1].value = " +
document.patien t.elements[1].value);

document.write( "\ndocument.pat ient.diet.optio ns[document.patien t.diet.selected Index].value
= " +
document.patien t.diet.options[document.patien t.diet.selected Index].value);
return;
Remove the return and instead add this:

document.close( );
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-98948567
}
</script>

<form name="patient">
Action attribute missing.
<p></p><hr>
empty paragraphs are usually a sign that a better use of css (padding or
margin) is welcomed. CSS should be used for presentational and/or
formating purposes; HTML should be used only for structuring the
document, the content.
Insanity checklist. Enter number of weeks: <input name="weeks" type="text">

I would add size and maxlength attributes to limit the user's input.
<p></p>
Clothing preferences:
<select name="diet">
<option value="napolean ">Dr Napolean</option>
<option value="darcy">D r Darcy</option>
<option value="strangel ove">Dr Strangelove</option>
</select>
<br>
<input type="submit" onclick="firstF unction(); return">
Remove the <br> and return here too.

<p><button type="submit" onclick="firstF unction();" accesskey="S">< span
style="text-decoration:unde rline; font-weight:bold;">S </span>ubmit
form</button></p>
I've never seen a document that modifies the document structure as
you're submitting a form; i'm not sure this is doable or correct. I
would rather use alert()'s to see these values instead.
If you want to validate the user's form data, then you still need to do
some coding: testing for empty string, test the weeks value (parseInt
the number of weeks), etc..
I would use accesskey and labels wherever it would be justified.
<hr><a href="./e.html">again</a><hr>
</form>


Not tested. I recommend you first validate your markup code.

DU

Jul 20 '05 #3
Randell D. wrote:
I have worked some of the code out - I figured the what should be working
below would be easy enough but again, I'm coming up with a surprise... I'm
not getting any errors... "firstFunction( )" manages to get the first
document.write to work, but the rest of it fails (ie no values, no errors).
Of course it fails. After the first document.write( ...)
there is no JavaScript code to be executed in the document
anymore.
[...]
<script language="JavaS cript">
<script type="text/javascript">
function firstFunction()
{
document.write( "<br>document.p atient.length = " + document.patien t.length);
document.write( "<br>document.p atient.weeks.va lue = " +
document.patien t.weeks.value);
document.write( "<br>document.p atient.elements[1].value = " +
document.patien t.elements[1].value);
return;
}
</script>
[...]
<input type="submit" onclick="firstF unction(); return">


http://groups.google.com/groups?as_q...ing=d&filter=0
See the third hit, for example.
PointedEars
--
Microsoft is sort of a mixture between the Borg and the Ferengi.
Combine the Borg marketing with Ferengi networking...
Jul 20 '05 #4

"Randell D." <re************ **********@and. share.com> wrote in message
news:yZgwb.4835 58$9l5.227310@p d7tw2no...

Whats wrong with my sample script below? All I want to do is confirm that
my form data is in my environment (basically, I'm writing a script that will check that some form fields have a value, and others don't - The
functionality of this will be used in my other script but I want to make
sure I know whats happening first as opposed to bastardizing someone else's work).

I have worked some of the code out - I figured the what should be working
below would be easy enough but again, I'm coming up with a surprise... I'm
not getting any errors... "firstFunction( )" manages to get the first
document.write to work, but the rest of it fails (ie no values, no errors).
Can someone help?

Much appreciated,
randelld

ps here's my code...

<script language="JavaS cript">
function firstFunction()
{
document.write( "<br>document.p atient.length = " + document.patien t.length); document.write( "<br>document.p atient.weeks.va lue = " +
document.patien t.weeks.value);
document.write( "<br>document.p atient.elements[1].value = " +
document.patien t.elements[1].value);
return;
}
</script>

<form name="patient">
<p></p><hr>
Insanity checklist. Enter number of weeks: <input name="weeks" type="text">
<p></p>
Clothing preferences:
<select name="diet">
<option value="napolean ">Dr Napolean</option>
<option value="darcy">D r Darcy</option>
<option value="strangel ove">Dr Strangelove</option>
</select>
<br>
<input type="submit" onclick="firstF unction(); return">
<hr><a href="./e.html">again</a><hr>
</form>


Thanks to all three of you who replied - I've noted your comments about my
misuse of <br> however some of you misunderstood my original post - The code
I provided was not going to be the exact code I would be using - It would
only prove what I wanted to do (ie entering data, pressing submit and then
displaying it in some readable format just to prove that I could re-display
form data... once I could do that, then my next step was to crunch/play with
the data... The code I had been using came from an old javascript book that
I am least likely to continue using... but I was sure I had enough
javascript skills in me to clean it up...

anyway... much appreciated for the help.. Since I made the post last night
I've actually taken a couple of steps forward. and your comments helped...

cheers
randell d.
Jul 20 '05 #5

"DU" <dr*******@hotW IPETHISmail.com > wrote in message
news:bp******** **@news.eusc.in ter.net...
Randell D. wrote:
Whats wrong with my sample script below? All I want to do is confirm that my form data is in my environment (basically, I'm writing a script that will check that some form fields have a value, and others don't - The
functionality of this will be used in my other script but I want to make
sure I know whats happening first as opposed to bastardizing someone else's work).

That's understandable.
I have worked some of the code out - I figured the what should be working below would be easy enough but again, I'm coming up with a surprise... I'm not getting any errors... "firstFunction( )" manages to get the first
document.write to work, but the rest of it fails (ie no values, no errors).
Can someone help?

Much appreciated,
randelld

ps here's my code...

<script language="JavaS cript">


Make sure you're using valid and validated markup code. Here, language
is deprecated; type has superseded language and is both backward and
forward-compatible. So

<script type="text/javascript">

function firstFunction()
{


document.open() ;
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-72161170
document.write( "<br>document.p atient.length = " +

document.patien t.length);
document.write( "\ndocument.pat ient.length = " + document.patien t.length);
document.write( "<br>document.p atient.weeks.va lue = " +
document.patien t.weeks.value);
document.write( "\ndocument.pat ient.weeks.valu e = " +
document.patien t.weeks.value);

document.write( "<br>document.p atient.elements[1].value = " +
document.patien t.elements[1].value);


document.write( "\ndocument.pat ient.diet.optio ns[document.patien t.diet.select
edIndex].value = " +
document.patien t.diet.options[document.patien t.diet.selected Index].value);

I've just tried using "\n" instead of "<br>" - Why would I use \n since it
did not place my data on a new line like it does when I use <br>?

randelld
Jul 20 '05 #6
DU
Randell D. wrote:
"Randell D." <re************ **********@and. share.com> wrote in message
news:yZgwb.4835 58$9l5.227310@p d7tw2no...
Whats wrong with my sample script below? All I want to do is confirm that
my form data is in my environment (basically, I'm writing a script that
will
check that some form fields have a value, and others don't - The
functionali ty of this will be used in my other script but I want to make
sure I know whats happening first as opposed to bastardizing someone


else's
work).

I have worked some of the code out - I figured the what should be working
below would be easy enough but again, I'm coming up with a surprise... I'm
not getting any errors... "firstFunction( )" manages to get the first
document.writ e to work, but the rest of it fails (ie no values, no


errors).
Can someone help?

Much appreciated,
randelld

ps here's my code...

<script language="JavaS cript">
function firstFunction()
{
document.write( "<br>document.p atient.length = " +


document.patien t.length);
document.write( "<br>document.p atient.weeks.va lue = " +
document.pati ent.weeks.value );
document.write( "<br>document.p atient.elements[1].value = " +
document.pati ent.elements[1].value);
return;
}
</script>

<form name="patient">
<p></p><hr>
Insanity checklist. Enter number of weeks: <input name="weeks"


type="text">
<p></p>
Clothing preferences:
<select name="diet">
<option value="napolean ">Dr Napolean</option>
<option value="darcy">D r Darcy</option>
<option value="strangel ove">Dr Strangelove</option>
</select>
<br>
<input type="submit" onclick="firstF unction(); return">
<hr><a href="./e.html">again</a><hr>
</form>


Thanks to all three of you who replied - I've noted your comments about my
misuse of <br> however some of you misunderstood my original post - The code
I provided was not going to be the exact code I would be using - It would
only prove what I wanted to do (ie entering data, pressing submit and then
displaying it in some readable format just to prove that I could re-display
form data... once I could do that, then my next step was to crunch/play with
the data...


I understood that.

The code I had been using came from an old javascript book that I am least likely to continue using... but I was sure I had enough
javascript skills in me to clean it up...

anyway... much appreciated for the help.. Since I made the post last night
I've actually taken a couple of steps forward. and your comments helped...

cheers
randell d.


All 3 of us have suggested that you use alert()'s instead of
document.write( ) calls. All 3 of us have suggested \n instead of a <br>
node inserted into a text string.

Finally, let me repeat again that you should first start with validating
your markup code, then (and only when such markup is entirely validated)
address the javascript issues. There are many reasons (including
debugging js and dhtml) and benefits for doing so.

My Web site is standard! And yours?
http://www.w3.org/QA/2002/04/Web-Quality

W3C validator
http://validator.w3.org/

DU

Jul 20 '05 #7
JRS: In article <bp**********@n ews.eusc.inter. net>, seen in
news:comp.lang. javascript, DU <dr*******@hotW IPETHISmail.com > posted at
Tue, 25 Nov 2003 01:16:12 :-

All 3 of us have suggested that you use alert()'s instead of
document.write () calls. All 3 of us have suggested \n instead of a <br>
node inserted into a text string.


But AFAICS none of you made it clear that <br> is what should normally
be used within document.write (though \n & \t can be useful within
<pre>) and \n is what should be used within such as alert or writing to
a control.

Within ordinary HTML, \n has no more effect than a space.

Basically, <br> is used within HTML, and \n within script; and the
ability to write HTML with script adds confusion.

Also : While a page is loading, document.write adds to the current
position. After a page is loaded, the next document.write starts a new
page, losing the old one.

The above is at least a first approximation.

Someone wrote "I've never seen a document that modifies the document
structure as you're submitting a form; i'm not sure this is doable or
correct. I would rather use alert()'s to see these values instead." --
it is presumably doable with DynWrite (see FAQ), and ISTM quite
practicable to have a form with a static entry area on the left and a
dynamic running-commentary at the right.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8

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

Similar topics

14
28139
by: Erik | last post by:
I need to submit a form using a hyperlink, and I need to pass a variable associated with that hyperlink. Example: I have a page where employees enter event information, and they are supplied a dropdown box to pick the promoter for that event. If the promoter does not exist in the database, they need to add him/her to the database so that the promoter can be associated with the event. If the promoter does not exist, I want to provide a...
12
8884
by: Forti2ude | last post by:
Hello, I have a simple form... <form> <select name="foo" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select>
2
3424
by: Vinita Sharma | last post by:
Hi All, I have a strange problem. I have 2 text boxes and a button in my form. There is a function called on onchange event of the first text box. There is another function called on onclick event of the button. Things work fine if you move from one field to another using tab keys. But if you change something in the first text box and move to button using mouse, the onchange event of the text box is called but the onclick event of the...
5
17712
by: johnsuth | last post by:
I want to produce a trivial demonstration of dynamic modification. I thought that pressing a button might change its color. I studied O'Reillys books and successfully created the button with a fancy style, but the onclick fails to do anything no matter what permutation of parameters I try. <input type=button style=background-color:yellow;color:blue;font-family:Arial;font-style:italic;font-weight:bold name="xyz" value="CHANGE COLOUR"...
5
2588
by: moondaddy | last post by:
I have a <a> element in a datagrid which wraps some asp.net labels. this element also has an onclick event which does not fire in netscape 6 (and perhaps other browsers for all I know...). Below is the code for this. the onclick event calls a javascript function which I put an alert in the firt line to tell me if its working. It does work in IE. Any ideas on how to get netcrap... oops, I'm sorry, netscape to fire the onclick event? ...
7
2795
by: extremerep | last post by:
My task is to change the value of a button and then make it functional with the onClick handler. Changing the value to "Play Again" works, but making the onClick work accordingly does not. The following is a snippet of the script. What is keeping it from working? function displaycards1(){ document.form1.reveal1.value="Play Again"; document.form1.reveal1.onClick="setcards();"; } </script>
2
7718
by: bay_dar | last post by:
Hi, I have an internal ASP.NET application that I'm are using to send e-mails out based on a single milepost or milepost range entered. I'm trying to do two things when a user clicks on the submit button to submit a form that contains one or two Mileposts: 1) If a Milepost range larger than 5 miles is entered, I would like to pop up a confirmation box to confirm the range.
1
4085
by: sourcie | last post by:
I am changing an existing quiz found on "JavaScriptKit.com Multiple Choice Quiz" I have an image. Instead of using the radio buttons with the normal true/false question, I want to place two hotspots on the image. One being correct(a) and the other incorrect(b). When the user clicks on the correct hotspot or place on the image, it should score and retain that value until the end of the quiz. At the end of the quiz, there is a submit button...
17
2684
by: yawnmoth | last post by:
http://www.frostjedi.com/terra/scripts/demo/this-alert.html http://www.frostjedi.com/terra/scripts/demo/this-alert2.html Why, when you click in the black box, do the alert boxes say different things? Shouldn't they say the same thing?
0
9647
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
10161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
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
9958
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
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
7506
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
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.