473,385 Members | 1,707 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,385 software developers and data experts.

How to access forms in Mozilla ?

Hi there,

I've got a strange problem.
Used to use IE.
Now switched to Mozilla (1.4).
This works fine in IE but produces nothing (not even an errormessage)
in Mozilla:

<HEAD>
......
<script language="JavaScript">
<!--
function calc()
{
var i;
var j;
i = calcform.T1.value;
j = 2*i;
calcform.T2.value = j;
}
-->
</script>
</HEAD>
<BODY>
<form name="calcform">
<input type='text' id="T1" value='2'>
<input type="button" id="B1" value="multiply by 2" onClick="calc()">
<input type='text' value='' id=T2>
</form>
.....

Mozilla seems to be unable to access the value of the first (or any)
input type of the form.

Is this a feature of Mozilla, or am I making a mistake somewhere?

Please help !!

Bart

--
Bart Broersma
br*********************@tiscali.nl
(ff _ANTISPAM_ wegpoetsen uit dit adres natuurlijk)
Jul 20 '05 #1
12 1337

"Bart" <br*********************@tiscali.nl> schreef in bericht
news:os********************************@4ax.com...

This works fine in IE but produces nothing (not even an errormessage)
in Mozilla:


The proper syntax in Mozilla is:
document.forms['formname'].elements['elementname']

This will also work in IE.

See: http://jibbering.com/faq/
JW

Jul 20 '05 #2
Bart wrote:
This works fine in IE but produces nothing (not even an errormessage)
in Mozilla:
Well, it *does* produce at least one error. Read
<3F**************@PointedEars.de>. Mozilla/3.0+
log script errors in their JavaScript console.
[...]
<script language="JavaScript">
<script type="text/javascript">
<!--
function calc()
{
var i;
var j;
i = calcform.T1.value;
j = 2*i;
calcform.T2.value = j;
}
-->
//-->

Otherwise the comment-closing `--' is considered the JavaScript
decrement operator.
[...]
Mozilla seems to be unable to access the value of the first (or any)
input type of the form.

Is this a feature of Mozilla, or am I making a mistake somewhere?
Yes :)

Try

function calc(oForm)
{
var j = 2 * oForm.elements["T1"].value;
oForm.elements["T2"].value = j;
return j; // a function should always return a value
}

...
<input
type="button"
id="B1"
value="multiply by 2"
onClick="calc(this.form)"


instead.
PointedEars
Jul 20 '05 #3
In article <3F**************@PointedEars.de>, Thomas 'PointedEars' Lahn
<Po*********@web.de> writes:
-->


//-->

Otherwise the comment-closing `--' is considered the JavaScript
decrement operator.


I beg to differ. If it were considered the decrement operator, then it would
throw an error since you can't decrement > and there is nothing after it to
decrement. That makes it an incomplete statement and as such - its an error.

But since no browser (modern) throws an error on it, then it must be safe to
assume that its *not* considered a decrement operator in that context and is
regarded as an incomplete closing comment.
--
Randy
Jul 20 '05 #4
Op Sat, 27 Dec 2003 02:49:47 +0100 schreef Thomas 'PointedEars' Lahn
<Po*********@web.de>:

<script type="text/javascript"> Yeah, use this syntax now in aal new scripts...

Try

function calc(oForm)
{
var j = 2 * oForm.elements["T1"].value;
oForm.elements["T2"].value = j;
return j; // a function should always return a value
}

...
<input
type="button"
id="B1"
value="multiply by 2"
onClick="calc(this.form)"
>


instead.
PointedEars


Works fine thanx.

This seems to work fine also though:

function calc()
{
var i = document.calcform.T1.value;
var j = 2*i;
document.calcform.T2.value = j;
}
Which syntax is the preferred one, or is this jus a matter of style
....

Thanks all of you.
Now I've been able to fix my 2 by 2 calculator for statistical
analysis.

Bart
--
Bart Broersma
br*********************@tiscali.nl
(ff _ANTISPAM_ wegpoetsen uit dit adres natuurlijk)
Jul 20 '05 #5
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
function calc(oForm)
{
var j = 2 * oForm.elements["T1"].value;
oForm.elements["T2"].value = j;
return j; // a function should always return a value
Could be shortened to
return oForm.elements["T2"].value *= 2;

However, what I really wanted to say was that a function should *not*
always return a value. It should only return a value when it makes
sense.

This function is being used in a context where its return value is
ignored, so there is no reason to complicate the code and make it
look like the return value is important, when it isn't.

And why that value? Why not the previous value of the input element?
If you can't answer that question, then there is no need for the
return at all.

(Or, with a different perspective: functions always return a value -
sometimes it's just the default "undefined")
<input .... onClick="calc(this.form)"
>


/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
Could be shortened to
return oForm.elements["T2"].value *= 2;


Forget that, I can't read well enough to distinguish T1 and T2 :P

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
function calc(oForm)
{
var j = 2 * oForm.elements["T1"].value;
oForm.elements["T2"].value = j;
return j; // a function should always return a value
Could be shortened to
return oForm.elements["T2"].value *= 2;

However, what I really wanted to say was that a function should *not*
always return a value. It should only return a value when it makes
sense.


Since a function call as an expression can be used in different
contexts, it is simply good style that is returns a useful value,
and may it be only a value indicating success or failure of the
performed operation(s).
(Or, with a different perspective: functions always return a value -
sometimes it's just the default "undefined")


And because `undefined' is not as easy to handle, a return value
is recommended. BTW: Mozilla/5.0's JavaScript console (rv:1.5+)
yields a warning if a function does not (always) return a value
-- for good reasons.
PointedEars
Jul 20 '05 #8
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
function calc(oForm)
{
var j = 2 * oForm.elements["T1"].value;
oForm.elements["T2"].value = j;
return j; // a function should always return a value
<snip>However, what I really wanted to say was that a function
should *not* always return a value. It should only return
a value when it makes sense.
Since a function call as an expression can be used in
different contexts,


In this respect I would go along with Lasse's "when it makes sense". If
a program wanted to know the value of twice the type-converted numeric
equivalent of the value in a form field then it would make sense to
write a function that read the field, did the math and returned the
value, but I don't see it making sense to assign that value to another
field and then return the value. To me that would suggest that the
function design was not suited to the task(s).

Take a function that sets the top, left, width and height of an
arbitrary absolutely positioned DIV element, a reasonable task to be
allotted to a function in many circumstances (especially if repeatedly
executed on differing DIVs). I cannot see any gain in returning any one
of the many possible individual values and I certainly would not want to
be executing such a heavyweight (in terms of the work the browser might
have to do as a consequence) function just to find out the value of
whatever it was that I had decided the function should return.

Functions may be used in different contexts and can be designed for that
use, but some functions should be designed for use in one context, it
does not make sense to be using them in other contexts and it would
probably be counterproductive to attempt to design them in a way that
attempted to make sense in other contexts.
it is simply good style that is returns a useful value,
Style to some extent will always be subjective. There are certainly
plenty of bad styles, we see them paraded through c.l.j on a daily
basis, so there must be better styles and should be good styles. But I
suspect that there cannot be a universal and absolute "correct" style.
Source code formatting is an example where "correct" style is to some
extent subjective. It is widely agreed that source code should be
indented to express its block structure, so code indenting is "good
style". But there are two widespread schools of code indenting (and
numerous less common variants) with the adherents to either recommending
(and teaching) that theirs is the best (possibly just because they find
it the easiest to quickly comprehend) but no real objective grounds for
choosing one over the other. Or at least, when I have read people
claiming that there are reasons for choosing one formatting style over
the other those reasons have never been backed up with anything
substantial or objective.

The use of return statements seems to fall into a similar area. For
example, it has been proposed that functions should not have multiple
exit points, only one return statement at the end (and possibly only
then if a value was to be explicitly returned). Similarly, it has been
proposed that it a statement uses a return statement in any branch of
its code it should have an explicit return statement at every exit
point. Obviously, being mutually exclusive, those two proposals could
not both represent an objectively correct approach to the use of return
statements.

Then there was the suggestion that object methods could always return -
this - whenever they were not returning something specific, so that the
objects could be interacted with using code such as:-

var x = objRef.doThis().doThat().doTheOther().getX();

-as opposed to:-

objRef.doThis();
objRef.doThat();
objRef.doTheOther();
var x = objRef.getX();

The nice thing about that last example was that it was expressed as a
personal style and accompanied with an explanation of the
consequences/uses of the resulting code. So it was very much up to the
reader to make an informed decision about its value and whether they
wanted to adopt it as a style, keep it in mind as a possible
method/strategy or forget about it entirely.
and may it be only a value indicating success or failure
of the performed operation(s).
When knowing the outcome of on operation is important/useful then
returning some indicating value is the obvious approach. But the
original function was using the value it was returning prior to any
consideration of the appropriateness of its use. So maybe it should have
been something more like:-

function twiceT1(oForm){
return (2 * oForm.elements["T1"].value);
}
....
var nTempValue = twiceT1(oForm);
if(!isNaN(nTempValue)){
oForm.elements["T2"].value = nTempValue;
}
....

- in which nobody would even question the return value in the -
twiceT1 - function, though it would still be questionable whether any
function containing the second block of code needed to be returning a
value.
(Or, with a different perspective: functions always return
a value - sometimes it's just the default "undefined")


And because `undefined' is not as easy to handle,


Undefined is not that difficult to handle, and if the function call has
no interest in the returned value it doesn't mater much what value it
was anyway. Granted, if the function was expected to return a value that
could be used as an indicator of its success/failure undefined would
probably never be the best value to be deliberately returning, but a
function that intended to return an object may still usefully return
undefined when it failed.
a return value is recommended.
In isolation that statement must be true just because you have
recommended it. But I don't think that you have backed that
recommendation up with anything more than personal opinion.
BTW: Mozilla/5.0's JavaScript console (rv:1.5+) yields a
warning if a function does not (always) return a value
Constructors do not necessarily (usually) have return statements and all
functions may be invoked as constructors, so can Mozilla really tell
when a function without a return statement is never a constructor (and
in any application rather than just the page context of its current
use)? Otherwise this sounds like a recipe for getting developers to
switch off the reporting of warnings in the JavaScript console to avoid
being swamped with what would be little more than uninformed opinion.

You seem to place a lot of faith in the authors of Mozilla. Personally I
find the claim that the Pope is granted divine infallibility more
credible than the implication that something is correct just because
that is the way it is implemented in Mozilla.
-- for good reasons.


A good reason usually being one that can be backed up with well
reasoned, substantial and convincing arguments.

Richard.
Jul 20 '05 #9
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Since a function call as an expression can be used in different
contexts, it is simply good style that is returns a useful value,
and may it be only a value indicating success or failure of the
performed operation(s).
The problem is that it is hard to guess what a useful value is, before
you actually need to use it. Failure or success is best indicated by a
boolean (unless you use exceptions, which is what they are there
for). As I said, a function that sets a property value would be more
useful returning the old value than the new ... but that would be
inconsistent with how the assignment operator works. And in object
oriented methodology, a setter usually doesn't return anything.

Just because a function *can* return a value, I don't think it should.
Just because the language doesn't have procedures (like Pascal) or
functions returning the void type (like Java), doesn't mean that you
should change the underlying design of your program.

A function should return a value only when that value is needed.
Everything else just adds clutter to the program and makes it harder
to maintain ... e.g., when the function later starts needing to
return a value that is different from the one you first guessed at.
And because `undefined' is not as easy to handle, a return value
is recommended.
How is it not easy to handle? It's a better return value than "null",
which is what I would otherwise suggest for a function with no obvious
return value.
BTW: Mozilla/5.0's JavaScript console (rv:1.5+)
yields a warning if a function does not (always) return a value
-- for good reasons.


I can't see that. Can you explain how to trigger such a warning?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Since a function call as an expression can be used in different
contexts, it is simply good style that is returns a useful value,
and may it be only a value indicating success or failure of the
performed operation(s).
The problem is that it is hard to guess what a useful value is, before
you actually need to use it.


Good design creates functions that are generally useful within their
context before they are actually used. The guess is not as hard as
you think.
Just because a function *can* return a value, I don't think it should.
Just because the language doesn't have procedures (like Pascal) or
functions returning the void type (like Java), doesn't mean that you
should change the underlying design of your program.
I like it when I am able to know if a function was successful (i.e. did
what it is supposed to do) or not, in the case I need that feedback for
further evaluation. For instance, I recently discovered to dislike the
addEventListener(...) method of the W3C-DOM Level 2 Core for neither
having a return value nor throwing an exception if unsuccessful. Such
is bad design.
A function should return a value only when that value is needed.
Everything else just adds clutter to the program and makes it harder
to maintain ... e.g., when the function later starts needing to
return a value that is different from the one you first guessed at.


That is what comes from bad design, from coding before thinking it
through thoroughly enough. A function should do what it is supposed
to do and then return yielding success, or do something else. If it
does something else, that something else should either be handled by
another function and the first function should return the success
status of the second function call, or the first function should
return yielding failure.
And because `undefined' is not as easy to handle, a return value
is recommended.


How is it not easy to handle? It's a better return value than "null",
which is what I would otherwise suggest for a function with no obvious
return value.


`undefined' is not as widely supported as `null' is
but both evaluate to `false' in conditional statements.
BTW: Mozilla/5.0's JavaScript console (rv:1.5+)
yields a warning if a function does not (always) return a value
-- for good reasons.


I can't see that. Can you explain how to trigger such a warning?


function foo(x)
{
if (x)
{
return;
}
else
{
return true;
}
}

Warning: function foo does not always return a value
Source File: javascript: function foo(x) { if (x) { return; } else {
return true; } }
Line: 1, Column: 56
Source Code:
function foo(x) { if (x) { return; } else { return true; } }
The warning is not yielded if one uses

function foo(x)
{
if (x)
{
return;
}
else
{
return;
}
}

though :-/
PointedEars
Jul 20 '05 #11
Bart wrote:
[...] Thomas 'PointedEars' Lahn wrote [...]:
function calc(oForm)
{
var j = 2 * oForm.elements["T1"].value;
oForm.elements["T2"].value = j;
return j; // a function should always return a value
}

...
<input
type="button"
id="B1"
value="multiply by 2"
onClick="calc(this.form)"
>

[...]


Works fine thanx.

This seems to work fine also though:

function calc()
{
var i = document.calcform.T1.value;
var j = 2*i;
document.calcform.T2.value = j;
}
Which syntax is the preferred one, or is this jus a matter of style


The former is standards-compliant (i.e. specified in W3C-DOM Level 2
HTML), the latter is proprietary. Since user agents become more and
more standards-compliant, you should use the former. Another advantage
of the former is that you do not have the form reference hard-coded
within local context but can pass *any* HTMLFormElement object, i.e.
you can rename the form without changing JavaScript code. You could
even add an argument to pass references to the element objects
currently known as `T1' and `T2' and then you would become independent
of any name or ID outside of intrinsic event handler attributes.
PointedEars

P.S.: Please trim your quotes.
Jul 20 '05 #12
Thomas 'Ingrid' Lahn wrote:
`undefined' is not as widely supported as `null' is
but both evaluate to `false' in conditional statements.


s/statements/expressions/
PointedEars
Jul 20 '05 #13

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

Similar topics

11
by: Google Mike | last post by:
I've got RH9 Linux with default PHP. Is there a way to send email on Linux to an Exchange Server from PHP and/or other tools when there is *NOT* SMTP access? Has anyone figured out a way to...
39
by: Zak McGregor | last post by:
Hi all Are there any good solutions to aligning form field names and input boxes without resorting to tables? I am struggling to do this nicely at the moment. Thanks Ciao Zak
4
by: Marc Elser | last post by:
Hi Everybody, Can someone please tell me how to access the form name if there's a form field named "name", for example: <form name="myform"> <input type="text" name="name" value="Marc">...
6
by: Jon Davis | last post by:
I recently learned how to do an <OBJECT> alternative to <IFRAME> in current browsers using: <object id="extendedhtml" type="text/html" data="otherpage.html" width="250" height="400"></object> ...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: Manuel | last post by:
My web app was running fine until I decided to change the custom errors parameter in the Web.config file. I set it to "On" and the app stopped working in IE but it works fine in Mozilla! So I...
1
by: littlefool | last post by:
Hi, I'm trying to access a XML file in Firefox. It works perfect in IE (with the ActiveXObject), but in Firefox displays it an empty page. I found on the web examples how to do that, but I still...
8
by: Behzad | last post by:
Hi all, Is there anybody out there who knows how to embed Mozilla engin in CSharp application. There are some useful articles in codeproject.com that show how to work around this issue with IE...
7
by: JDOMPer | last post by:
Don’t misunderstand me – I use AJAX, but I think there is a far simpler, elegant alternative that just uses Javascript, the DOM and Php ( hence - JDOMP) for data transfers, and is cross-browser...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...

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.