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

Javascript Copy Text Boxes..I'm being stupid!

@sh
I know this is probably a real simple one, but I'm obviously missing
something..

I'm building a function that I'll use throughout a website in the situation
that I have two text boxes - the two text boxes will generally contain the
same data. After the user completes the value of the first textbox, I want
to onChange the value of the first textbox into the second textbox UNLESS
the second textbox already has a value.

Here's what I've done so far...

IN THE HEAD
function CopyTextBoxes(TextBox1,TextBox2) {
if(TextBox2.value = '') {
TextBox2.value = TextBox1.value}
}

IN THE BODY WITHIN THE FORM
<input name="MailFrom" type="text" id="MailFrom" size="60" maxlength="100"
onChange="CopyTextBoxes('MailReplyTo','MailFrom')" >
<input name="MailReplyTo" type="text" id="MailFrom" size="60"
maxlength="100">

I hate javascript, and will really appreciate your help!

Thanks!

Dec 13 '05 #1
6 2047
Rob
@sh wrote:
I know this is probably a real simple one, but I'm obviously missing
something..

I'm building a function that I'll use throughout a website in the
situation that I have two text boxes - the two text boxes will generally
contain the same data. After the user completes the value of the first
textbox, I want to onChange the value of the first textbox into the second
textbox UNLESS the second textbox already has a value.

Here's what I've done so far...

IN THE HEAD
function CopyTextBoxes(TextBox1,TextBox2) {
if(TextBox2.value = '') {
TextBox2.value = TextBox1.value}
}

IN THE BODY WITHIN THE FORM
<input name="MailFrom" type="text" id="MailFrom" size="60" maxlength="100"
onChange="CopyTextBoxes('MailReplyTo','MailFrom')" >
<input name="MailReplyTo" type="text" id="MailFrom" size="60"
maxlength="100">

I hate javascript, and will really appreciate your help!

Thanks!

I've always found weird/unexpected problems with different browsers when
using foo.value. I would make both ID's unique and use
document.getElementById(TextBox2).value =
document.getElementById(TextBox1).value;
Dec 13 '05 #2
Rob wrote:
@sh wrote:
[...]
IN THE HEAD
function CopyTextBoxes(TextBox1,TextBox2) {
if(TextBox2.value = '') {
TextBox2.value = TextBox1.value}
}

IN THE BODY WITHIN THE FORM
<input name="MailFrom" type="text" id="MailFrom" size="60"
maxlength="100" onChange="CopyTextBoxes('MailReplyTo','MailFrom')" >
<input name="MailReplyTo" type="text" id="MailFrom" size="60"
maxlength="100">
If the intent is to copy the value of the control named `MailReplyTo' to
the control named `MailFrom', as the code suggests, then the event handler
of the wrong control is used. Think about it: if `MailFrom' changes, the
event fires. But every change is also a change of value, so the value just
changed will be overwritten by the value of `MailReplyTo'. (If instead
`MailFrom' should be read-only, then the appropriate attribute should be
set, and no client-side scripting at all would be required.)

If instead the intent is to copy the new value of `MailFrom' (the
control that fires the event) to `MailReplyTo', the copy direction
needs to be _reversed_.

Since the former does not make much sense to me, I assume that the latter
applies.
I hate javascript, and will really appreciate your help!

Post to comp.lang.i-hate-javascript instead!
I've always found weird/unexpected problems with
different browsers when using foo.value.
Could you please elaborate?
I would make both ID's unique
Of course, as they have to.
and use
document.getElementById(TextBox2).value =
document.getElementById(TextBox1).value;


No, use unique names for the `elements' collection instead.
Quickhack:

function copyValue(textBox1, textBox2)
{
if (textBox1 && textBox2)
{
var f, es;
if (typeof textBox1 == "string"
&& document.forms
&& (f = document.forms[0])
&& (es = f.elements))
{
textBox1 = es[textBox1];
}

if (textBox1)
{
if (typeof textBox2 == "string"
&& (f = textBox1.form
|| (document.forms && (f = document.forms[0])))
&& f && (es = f.elements))
{
textBox2 = es[textBox2];
}

if (textBox2)
{
textBox2.value = textBox1.value;
}
else
{
alert('Error: target does not exist.');
}
}
else
{
alert('Error: source does not exist.');
}
}
else
{
alert('Error: source and target must not be empty/null.');
}
}

<form ...>
<input name="MailFrom" size="60" maxlength="100"
onchange="copyValue(this, 'MailReplyTo')">
<input name="MailReplyTo" size="60" maxlength="100">
</form>

Note that if the names/IDs used here are semantic, RFC2822 "Internet
Message Format" does not require the Reply-To header value to be set
the same as the From header value. User agents have to use and will
use the From header value automatically for the To header of replies
if Reply-To is missing.
PointedEars
Dec 13 '05 #3
@sh wrote:
I know this is probably a real simple one, but I'm obviously missing
something..
Looks like :)
I'm building a function that I'll use throughout a website in the situation
that I have two text boxes - the two text boxes will generally contain the
same data. After the user completes the value of the first textbox, I want
to onChange the value of the first textbox into the second textbox UNLESS
the second textbox already has a value.

Here's what I've done so far...

IN THE HEAD
function CopyTextBoxes(TextBox1,TextBox2) {
if(TextBox2.value = '') { **** here is your problem
TextBox2.value = TextBox1.value}
}


Your if statement is SETTING TextBox2.value to '', which returns true,
and then sets TextBox2.value.

What you want is to TEST the value:

if(TextBox2.value == '') ...

Note the double-equal sign. That is how to check for equality in
conditionals.

That one gets me all the time, actually - it's easy to miss typing the
second equal when you're going fast...

Dec 13 '05 #4
Tony wrote:
@sh wrote:
IN THE HEAD
function CopyTextBoxes(TextBox1,TextBox2) {
if(TextBox2.value = '') { **** here is your
problem
TextBox2.value = TextBox1.value}
}
Your if statement is SETTING TextBox2.value to '',


Ahh, I missed that.
which returns true,
It does not return anything. You describe JavaScript 1.2-specific
behavior as implemented in Netscape Navigator 4.0 to 4.05 (June 1997)
and in other browsers if <script ... language="JavaScript1.2"> is used.
There the AssignmentExpression _evaluates to_ `true' if successful,
`false' otherwise.

However, version/implementation-unspecific behavior is that TextBox2.value
is indeed assigned a value; that value is the empty string which
type-converts to `false', ...
and then sets TextBox2.value.
.... therefore the block is _never_ executed and TextBox2.value is _never_
assigned TextBox1.value.
What you want is to TEST the value:

if(TextBox2.value == '') ...

Note the double-equal sign. That is how to check for equality in
conditionals.
[...]


It still does not make sense, see news:14****************@PointedEars.de
PointedEars
Dec 13 '05 #5
Thomas 'PointedEars' Lahn said the following on 12/13/2005 1:55 PM:
Tony wrote:

@sh wrote:
IN THE HEAD
function CopyTextBoxes(TextBox1,TextBox2) {
if(TextBox2.value = '') { **** here is your
problem
TextBox2.value = TextBox1.value}
}


Your if statement is SETTING TextBox2.value to '',

Ahh, I missed that.

which returns true,

It does not return anything. You describe JavaScript 1.2-specific
behavior as implemented in Netscape Navigator 4.0 to 4.05 (June 1997)
and in other browsers if <script ... language="JavaScript1.2"> is used.
There the AssignmentExpression _evaluates to_ `true' if successful,
`false' otherwise.

However, version/implementation-unspecific behavior is that TextBox2.value
is indeed assigned a value; that value is the empty string which
type-converts to `false', ...


You are not testing the .value of that input. The test, as above, is
testing to see if you can set it's value to '', which you can, which
evaluates to true.

var myVar;
if (myVar = '3'){
alert('This alert will always fire in a decent UA')
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 13 '05 #6
Randy Webb wrote:
Thomas 'PointedEars' Lahn said the following on 12/13/2005 1:55 PM:
Tony wrote:
@sh wrote:
IN THE HEAD
function CopyTextBoxes(TextBox1,TextBox2) {
if(TextBox2.value = '') { **** here is your
problem
TextBox2.value = TextBox1.value}
}

Your if statement is SETTING TextBox2.value to '',
Ahh, I missed that.
which returns true,


It does not return anything. You describe JavaScript 1.2-specific
behavior as implemented in Netscape Navigator 4.0 to 4.05 (June 1997)
and in other browsers if <script ... language="JavaScript1.2"> is used.
There the AssignmentExpression _evaluates to_ `true' if successful,
`false' otherwise.

However, version/implementation-unspecific behavior is that
TextBox2.value is indeed assigned a value; that value is the empty
string which type-converts to `false', ...


You are not testing the .value of that input. The test, as above, is
testing to see if you can set it's value to '', which you can, which
evaluates to true.


That is so in JavaScript 1.2 _only_. Other versions and implementations
will evaluate that to `false' because the empty string is a false-value:

var myVar;
if (myVar = '')
{
alert('This alert will be displayed with JavaScript 1.2 only.');
}
var myVar;
if (myVar = '3'){
alert('This alert will always fire in a decent UA')
}


Please read more carefully. Tony, and you in your first paragraph,
describe what I describe as being JavaScript 1.2-specific behavior
in my first paragraph. However, what is more likely to happen is
the version/implementation-unspecific behavior I described in my
second one.

In your second paragraph you are mixing things up; of course this will
always fire, because in JavaScript 1.2 the assignment is successful and,
for other versions/implementations, '3' is a true-value.

It was already clarified by Tony that the assignment, which should
have been a comparison, was the problem; what was left to be clarified
by me was the interpretation of the assignment. Your posting did not
reveal any new facts.
PointedEars
Dec 14 '05 #7

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

Similar topics

0
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass...
13
by: Sheperd | last post by:
Hey everyone, I have this browser detect script and the validator is having problems with it, im trying to validate a page for XHTML 1.0 Transitional If you have any ideas or suggestions they...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
19
by: Fabian | last post by:
Well, what can I say...I really hate Javascript. I am not programming as profession but I sometimes work with PHP with MySQL, ASP with Ms Access/SQL Server. I have done something in VB and when...
5
by: DigitalGENOcyde | last post by:
I am fairly new to working with programming so pardon my lack of knowledge, but I was hoping someone could assist me with working on a project. I am trying to make a Windows application using...
2
by: @sh | last post by:
I know this is probably a real simple one, but I'm obviously missing something.. I'm building a function that I'll use throughout a website in the situation that I have two text boxes - the two...
36
by: Robert Baer | last post by:
I used Google and found some references for integer in Java. But "int" not only does not work, it also prevents reading X and Y coordinates of the mouse. What i would like to do: 1) Get X and Y...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
8
by: jh | last post by:
I'd like to copy/paste into a listbox during runtime. I can do this for a textbox but can't figure out how to accomplish this for a listbox. Any help? Thanks.
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.