Value of string???

C

ckerns

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
 
R

RobG

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 )
 
D

Dietmar Meier

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
 
C

ckerns

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
 
R

RobG

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
}
}


[...]
 
D

Dr John Stockton

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 said:
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.
 
R

RobG

Dr said:
JRS: In article <42625d97$0$9230$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sun, 17 Apr 2005 22:56:17, seen in



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...
 
B

Brian Munroe

RobG said:
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>
 
D

Dietmar Meier

Brian said:
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
 
D

Dr John Stockton

JRS: In article <W0%[email protected]>, dated Tue, 19
Apr 2005 03:36:22, seen in RobG
Dr John Stockton wrote:

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top