Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One value needs to appear on two places on my form.

Status
Not open for further replies.

programmher

Programmer
May 25, 2000
235
0
0
US
I am passing some values from one form to another like so:

window.opener.document.formname.name.value = name;


My problem is that I have two places on my original form that I need "name" to appear every time it is changed or selected.

I can always get one to appear but not the other. Does anyone know how to do this?
 
window.opener.document.formname.name1.value = name;
window.opener.document.formname.name2.value = name;
=========================================================
while (!succeed) try();
-jeff
 
building upon jemmingers already excellent code :

with (window.opener.document.formname)
{
name1.value = name;
name2.value = name;
} Gary Haran
 
true Gary, but i've read that with() is very inefficient. haven't done any benchmarking of it though...care to accept the challenge? ;-)
=========================================================
while (!succeed) try();
-jeff
 
Jemminger,

You are right using with does make things slightly slower but I found that sometimes it is good to have such options available to you in two cases.

In a rare scenario where you have hundreds of form items to be redirected using this system the amount of text being sent could be much smaller if with() is used. I'll admit that it only happened to me once that with() was useful for that reason to me and the circumstances were really not working in my favor (400 form items on a web application and using server side zlib compression was out of the question because of a bug with Real Media Player and Windows).

I have used it a few times because I was lazy and didn't want to type too much (if I have to type the thing more than ten times I use with() unless speed of execution is primordial).

using Phoenix

using with() : 1161 milliseconds
without with() : 1042 milliseconds

using Internet Exploder

using with() : 400 milliseconds
without with() : 400 milliseconds

using Mozilla (latest build)

using with() : 1151 milliseconds
without with() : 952 milliseconds

Using Opera 7

using with() : 231 milliseconds (wow!!!)
without with() : 200 milliseconds (wow!!!)

I have to admit I am really impressed with the raw speed of Opera here! More power to Opera users! :)

This is the code I used to do the tests. Each test was run 5 times and I took the lowest number for each browser.

<html>
<head>
<title>Form element referencing using with()</title>

</head>
<body>

<form name=&quot;myForm&quot;>
<input type=&quot;text&quot; id=&quot;myInput&quot; value=&quot;let's test this baby&quot;>
<input type=&quot;button&quot; value=&quot;test using with()&quot; onclick=&quot;test1()&quot;>
<input type=&quot;button&quot; value=&quot;test using direct path&quot; onclick=&quot;test2()&quot;>
</form>

<script>
var iterations = 5000;

function test1()
{
var start = new Date();
for (var i = 0; i < iterations; i++)
{
with (document.myForm.myInput)
{
value = &quot;testing&quot;;
}
}
var end = new Date();
document.myForm.myInput.value = end.getTime() - start.getTime();
}

function test2()
{
var start = new Date();
for (var i = 0; i < iterations; i++)
{
document.myForm.myInput.value = &quot;testing&quot;;
}
var end = new Date();
document.myForm.myInput.value = end.getTime() - start.getTime();
}
</script>


</body>
</html> Gary Haran
 
interesting...i think i'll try some tests too out of curiosity.

what about using a var to store the form?

var f = document.forms[0];
f.name1.value = name;
f.name2.value = name;
=========================================================
while (!succeed) try();
-jeff
 
hmm...seems that IE is the dog using with() in my test:

IE 6.0
using with(); 1773 ms.
not using with(); 701 ms.

Moz 1.3
using with(); 371 ms.
not using with(); 250 ms.

Phoenix 0.5
using with(); 431 ms.
not using with(); 330 ms.

Don't have Opera (yet...downloading now :) )

here's the test I used, with 400 form elements:

[tt]
<html>
<head>
<title></title>

<script language=&quot;javascript&quot;>
function check(f) {
// test using with();
var s1 = new Date();
with(f) {
for (var x = 0; x < f.elements.length; x++)
var temp = elements[x].value;
}
var e1 = new Date();

// test not using with();
var s2 = new Date();
for (var x = 0; x < f.elements.length; x++)
var temp = f.elements[x].value;
var e2 = new Date();

document.getElementById(&quot;results&quot;).innerHTML += 'using with(); ' + (e1 - s1) + ' ms.' +
'<br/>not using with(); ' + (e2 - s2) + ' ms.<p/>';
}
</script>

</head>

<body>
<form>
<script language=&quot;javascript&quot;>
for (var x = 0; x < 401; x++)
document.writeln('<input type=&quot;text&quot; name=&quot;t' +x+ '&quot; value=&quot;' +x+ '&quot; size=&quot;3&quot;/>');
</script>
<input type=&quot;button&quot; value=&quot;check();&quot; onclick=&quot;check(this.form);&quot; />
</form>
<div id=&quot;results&quot;></div>
</body>
</html>
[/tt]
=========================================================
while (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top