I have a form with 40 text boxes (default of each = 0). I want to
evaluate each box to see if the value is still zero, and if it is, I
want to add it to a "Zero Count". The box names are "L411 thru L419",
"L421 thru L429", "L431 thru L439" and "L441 thru L449".
Example:
L41 - 002000500
L42 - 000030000
L43 - 000000000
L44 - 000000000
I have construct the control name thru a couple of nested for loops
and all is well (constructed string = "MyForm.L411.value").
When I plug this into an If statement to get the value of the control,
everything dies. Here is my If:
for(var j = 1; j < 5; j++)
{
rowString = "L4";
// builds L41
rowString = rowString + j;
for(var x = 1; x < 10; x++)
{
// builds L411
rowString = rowString + x;
// Builds MyForm.L411.value
rowString = "MyForm." + rowString + ".value"
//alert shows "MyForm.L411.value" - without quotes
//alert(rowString);
if( rowString = "0")
{
zeroCount++:
}
rowString = "L4" + j;
}
}
How can I test the value of a control, based on a string built?
Many tia's
ck 11 1419 ck****@fusert.net wrote: I have a form with 40 text boxes (default of each = 0). I want to evaluate each box to see if the value is still zero, and if it is, I want to add it to a "Zero Count". The box names are "L411 thru L419", "L421 thru L429", "L431 thru L439" and "L441 thru L449".
Example:
L41 - 002000500 L42 - 000030000 L43 - 000000000 L44 - 000000000
I have construct the control name thru a couple of nested for loops and all is well (constructed string = "MyForm.L411.value").
When I plug this into an If statement to get the value of the control, everything dies. Here is my If:
for(var j = 1; j < 5; j++) { rowString = "L4";
// builds L41 rowString = rowString + j;
for(var x = 1; x < 10; x++) { // builds L411 rowString = rowString + x;
// Builds MyForm.L411.value rowString = "MyForm." + rowString + ".value"
Ooops... you have made 'rowString' the literal string
"MyForm.L411.value" which is not what you want to do.
What you want is:
rowString = MyForm.rowString.value;
presuming that the form has an element with a name equal to the value
of 'rowString', i.e. L411.
But this is dangerous, test that the element exists before trying to
get its value, 'cos if it doesn't, your script will die:
if ( MyForm.rowString ) {
rowString = Myform.rowString.value;
} else {
rowString = undefined;
}
which can be abbreviated to:
rowString = ( MyForm.rowString && MyForm.rowString.value );
I would suggest changing the name to rowValue, but that's just me.
Reusing variables this way may make maintenance more difficult, but
again, that's up to you. It may be better to build up the element
name in a variable called 'eleName', then assign the value to
'eleValue' so things are a bit clearer and you'd get:
eleValue = ( MyForm.eleName && MyForm.eleName.value ); //alert shows "MyForm.L411.value" - without quotes //alert(rowString);
if( rowString = "0")
Instead of testing whether the value of 'rowString' is equivalent to
the string '0', you are assigning it the value of the string '0'.
As a hint always write such tests:
if( '0' = rowString )
That way you will get a script error rather than just erroneous
results. The correct syntax (assuming you want to test for equality)
is:
if( '0' == rowString )
{ zeroCount++:
} rowString = "L4" + j; } }
How can I test the value of a control, based on a string built?
Many tia's
ck
--
Rob ck****@fusert.net wrote: I have a form with 40 text boxes (default of each = 0). I want to evaluate each box to see if the value is still zero, and if it is, I want to add it to a "Zero Count". The box names are "L411 thru L419", "L421 thru L429", "L431 thru L439" and "L441 thru L449".
(That are 36, not 40, text inputs).
var i, j, nZeroCount, oForm, oCurElem, oCurVal;
oForm = document.forms['MyForm'];
nZeroCount = 0;
for (i=1; i<10; i++) {
for (j=1; j<5; j++) {
oCurElem = oForm.elements["L4" + j + i];
if (oCurElem) {
oCurVal = oCurElem.value;
if (!isNaN(oCurVal) && parseInt(oCurVal, 10) == 0) {
nZeroCount++;
}
}
}
}
ciao, dhgm
> When I plug this into an If statement to get the value of the control, everything dies. Here is my If:
if( rowString = "0")
If you had used JSLint, it would have alerted you to this statement. You
probably meant
if (rowString == "0") {
See http://www.JSLint.com
Thank you very much! That got me on the right track now! :)
Although, if I use basically the same loop earlier in the structure,
how can I assign a "0" to non-numeric entries? Granted what I'm
checking for (below) is null, but I don't think that is exactly what
I want to do. I believe I should be checking for "non-numeric"
instead.
This is what I am trying, but it doesn't work :(
for(var j = 1; j < 5; j++)
{
rowString = "L4";
rowString = rowString + j;
for(var x = 1; x < 10; x++)
{
rowString = rowString + x;
if (eval ("document.forms[0]." + rowString + ".value") ==
Null)
{
MyForm.rowstring.value = 0;
}
rowString = "L4" + j;
}
}
ck ck****@refuse.net wrote: Thank you very much! That got me on the right track now! :)
Although, if I use basically the same loop earlier in the structure, how can I assign a "0" to non-numeric entries? Granted what I'm checking for (below) is null, but I don't think that is exactly what I want to do. I believe I should be checking for "non-numeric" instead.
This is what I am trying, but it doesn't work :(
for(var j = 1; j < 5; j++) { rowString = "L4"; rowString = rowString + j;
Save yourself some typing, two lines to one:
rowString = "L4" + j; for(var x = 1; x < 10; x++) { rowString = rowString + x;
or
rowString += x;
Since rowString is a string, x will be concatenated (i.e. appended)
to the string.
if (eval ("document.forms[0]." + rowString + ".value") == Null)
Ditch 'eval', it is nearly never, ever needed.
The value of an empty input is '' (empty string), not 'null' (note
captialisation).
I'll assume that what you want to do is see if the value is zero. If
so, add it to the zero count. If it's only digits, leave it alone.
If it's anything else, give an error. So here goes:
// Mentioned sensible variable names before
var eleValue;
// Make sure the element exists, then get its value
if ( document.forms[0].rowString
&& ( eleValue = document.forms[0].rowString.value )) {
// If the value is zero, add one to zeroCount
if ( eleValue == 0 ){
zeroCount++;
// If there is anything in the value that isn't 0-9
} else if ( /\D/g.test(eleValue) ) {
// Do whatever needs to be done if there are
// things in the value that aren't digits
}
}
[...]
--
Rob
JRS: In article <42625d97$0$9230$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sun, 17 Apr 2005 22:56:17, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.auau> posted : Ditch 'eval', it is nearly never, ever needed.
Yes and no. One rarely needs to write it; OTOH, it and function call
are probably the two statements that I execute most frequently.
<URL:http://www.merlyn.demon.co.uk/js-quick.htm>; View Source, or enter
remoteEval.toString()
in the textarea and press RmEv !
BTW, could someone(s) look at
<URL:http://www.merlyn.demon.co.uk/js-boxes.htm>
with non-MSIE to see if all is reasonably well?
The main thing is the box starting -- New Code : in which ShoTrim
will I hope be shown as containing the string "' comment ? '" and
ShoLinCnt's first line should be the only lone that wraps and a line
-- One visible blank line next; scroll for more code :
should be true. And function NotALot should fit the next box.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dr John Stockton wrote: JRS: In article <42625d97$0$9230$5a62ac22@per-qv1-newsreader- 01.iinet.net.au>, dated Sun, 17 Apr 2005 22:56:17, seen in news:comp.lang.javascript, RobG <rg***@iinet.net.auau> posted :
Ditch 'eval', it is nearly never, ever needed.
Yes and no. One rarely needs to write it; OTOH, it and function call are probably the two statements that I execute most frequently.
<URL:http://www.merlyn.demon.co.uk/js-quick.htm>; View Source, or enter remoteEval.toString() in the textarea and press RmEv !
BTW, could someone(s) look at <URL:http://www.merlyn.demon.co.uk/js-boxes.htm> with non-MSIE to see if all is reasonably well?
Gosh, nearly missed you way down here! The main thing is the box starting -- New Code : in which ShoTrim will I hope be shown as containing the string "' comment ? '" and
Nope, does not appear. Comparison with IE shows it to be totally
missing in Firefox.
ShoLinCnt's first line should be the only lone that wraps and a line
The comment that causes the line to wrap in IE is not there in
Firefox.
-- One visible blank line next; scroll for more code : should be true.
Yes, but the IE code is missing a few linefeeds after semi-colons
that are in Firefox and comments vanish:
FF:
function ShoDoo(Fn) {
var St = ShoTrim(Fn.toString());
Depict(BoxX, ShoLinCnt(St, BoxX), St, "red");
Fn();
return "";
IE:
function ShoDoo(Fn) { // N.B. this calls Fn() - ADD show others ?
var St = ShoTrim(Fn.toString())
Depict(BoxX, ShoLinCnt(St, BoxX), St, "red") ; Fn() ; return "" }
And function NotALot should fit the next box.
FF:
function NotALot() {
}
Yup, that's not a lot!! The comments show up in Opera though.
:-)
In general, Opera does it all differently again - dropping some
comments, adding in new lines and dropping others...
--
Rob
RobG wrote: Ditch 'eval', it is nearly never, ever needed.
[snip]
Hi, I don't mean to hijack the thread to start beating a dead horse,
but I can't get your eval replacement code to work. I think I
implemented it correctly in the sayHi() function. eleValue is always
returned undefined, any suggestions?
thanks
-- brian
<html>
<head>
<script type="text/javascript">
function sayHi() {
var eleValue;
var rowString = "txt1";
if ( document.forms[0].rowString && ( eleValue =
document.forms[0].rowString.value )) {
alert("hello");
} else {
alert(eleValue);
}
}
function sayHi2() {
var rowString = "txt1";
res = eval("document.forms[0]." + rowString + ".value");
alert(res);
}
</script>
</head>
<body>
<form name="main">
<input type="text" name="txt1" size="10" value="hi" />
<input type="button" onclick="sayHi();" />
</form>
</body>
</html>
Brian Munroe wrote: var rowString = "txt1"; if ( document.forms[0].rowString && ( eleValue = document.forms[0].rowString.value )) { alert("hello"); } else { alert(eleValue); } }
var rowString = "txt1";
if (document.forms[0][rowString]
&& (eleValue = document.forms[0][rowString].value)
) {
alert("hello");
}
else {
alert(eleValue);
}
ciao, dhgm
Dietmar Meier wrote: if (document.forms[0][rowString] && (eleValue = document.forms[0][rowString].value)
ciao, dhgm
Well that was easy enough!
thanks
-- brian
JRS: In article <W0****************@news.optus.net.au>, dated Tue, 19
Apr 2005 03:36:22, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted : Dr John Stockton wrote:
BTW, could someone(s) look at <URL:http://www.merlyn.demon.co.uk/js-boxes.htm> with non-MSIE to see if all is reasonably well?
The main thing is the box starting -- New Code : in which ShoTrim will I hope be shown as containing the string "' comment ? '" and
Nope, does not appear. Comparison with IE shows it to be totally missing in Firefox.
Disappointing but not surprising.
An unused value is optimised out in Firefox.
After
function f() { x = "'comment'" ; var y = "'remark'" ; return 5 }
does f.toString() show the 'comment' and 'remark' in Firefox?
Delphi, at least, would optimise away those statements. -- One visible blank line next; scroll for more code : should be true.
Yes, but the IE code is missing a few linefeeds after semi-colons that are in Firefox
NO !! Firefox has inserted two linefeeds and a semi-colon ! My wish,
after all, is that the actual source be shown; and IE does that almost
perfectly (long white-space can, in IE4, cause premature wrapping).
In general, Opera does it all differently again - dropping some comments, adding in new lines and dropping others...
Still, you've seen nothing too bad. Thanks.
ISTM that I need a test case with a single statement that exceeds 68 or
69 characters, though I hope never to need to write one. One is now
added to function NotALot.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sam |
last post by:
Hi,
I m wondering why I can't declare reference variable for default value
in a function argument variable?
eg.
class A
{
void f(string &str="");
|
by: LRW |
last post by:
Below is some of the code I'm using.
I have a PHP page generating a list of items. I've made it so that
each one has a radiobutton with a unique value. You can click on the
radiobutton and it will...
|
by: Grant Wagner |
last post by:
I'm a bit confused by String() (typeof 'string') vs new String() (typeof
'object'). When you need to access a method or property of a -String-,
what type is JavaScript expecting (or rather, what...
|
by: Webgour |
last post by:
How to go from string "abc" to string "a|b|c"?
|
by: Chris Simmons |
last post by:
I know that a String is immutable, but I don't understand why this
piece of code fails in nUnit:
// BEGIN CODE
using System;
class Test
{
public static void Main( String args )
|
by: Steven Blair |
last post by:
I have the following string:
<CardDetails>4561</CardDetails><Message Identifier>1</Message
Identifier><TID>88888888</TID>
I need to be able to extract the values from the tags easily in C#....
|
by: Zontar |
last post by:
I'm trying to improve performance on a query, and I was wondering if this is possible in Access.
Let's say I have a table with one text column and one row.
In that column, I have a field name...
|
by: Nick Valeontis |
last post by:
code:
---------------- ---------------- ---------------- --------------------------------
----------------
namespace TestApp {
public partial class Form1 : Form {
private object s = "1";
...
|
by: Andrus |
last post by:
I need to create generic table field level cache.
Table primary key (PrimaryKeyStructType) can be int, string or struct
containing int and string fields.
FieldName contains table field name to be...
|
by: sumanta123 |
last post by:
Dear Sir,
In my develpment i am getting stuck for a senario.Kindly please help me for it.
Everytime we give the request we get the response of 8 records and
its corresponding value.
Then next...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| |