...

View Full Version : copy value of one field to another



ventura
04-04-2003, 07:32 PM
i tried to write a generic function that i can use to copy the value from one field to another like below, but i get an error that says that "str.orig has no properties". any idea how i can do this or what i am doing wrong?

i'm trying to copy the value from the contact_email field to the logininfo_username field.



<input type=text name="contact_email" onKeyUp="copyFieldValue(contact_email,logininfo_username);">


function copyFieldValue(orig,copyTo) {
var str = document.form1;

str.copyTo.value = str.orig.value;
}

beetle
04-04-2003, 08:12 PM
str.elements[copyTo].value = str.elements[orig].value;

newboy
04-04-2003, 08:36 PM
<script>
function copyit(theField) {
var tmp=eval(theField)
therange=tmp.createTextRange()
therange.execCommand("Copy")
}
function paste(theField)
{
var tmp=eval(theField)
range=tmp.createTextRange()
range.execCommand("Paste")
}
</script>
<input type="button" onclick="copyit('select1');paste('select2')" value="Test" >
<input type="Text" name="select1" onkeydown="copyit('select1');paste('select2')">
<input type="Text" name="select2">

beetle
04-04-2003, 09:00 PM
eval == evil



:D :D

liorean
04-04-2003, 09:07 PM
Beetle - how about
(new Function([string anythingYouCanEval]))();

beetle
04-04-2003, 09:16 PM
Hmmm, creating serialized functions is a pretty rare occurance. One of the best applications I've seen is a constructor constructor.
function ClassConstructor( props, methods )
{
var prop, meth, fstr='';
for ( i=0; (prop = props[i]); i++ )
{
fstr += 'this.' + prop + '=' + prop + ';';
}
for ( i=0; ( meth = methods[i] ); i++ )
{
fstr += 'this.' + meth + '=' + meth + ';';
}
return new Function( props, fstr );
}

function pricePerUnit()
{
return Math.round( ( this.price / this.quantity ) * 100 ) / 100;
}

dataprops = ['id','name','price','quantity'];
datamethods = ['pricePerUnit'];

var ShoppingItem = new ClassConstructor( dataprops, datamethods );
var myData = new ShoppingItem( 1, 'x-widget', 9.99, 12 );
alert( "The " + myData.name + " has a per unit cost of $" + myData.pricePerUnit() + " dollars." );Can't say I've ever used it though.

I actually use the Function constructor in my newest version of fValidate :D

liorean
04-04-2003, 09:20 PM
Brothercake (Ok, Beetle, then ;) ) - you failed to see my point - I used it as were it eval. Try it:
(new Function('alert(\\'instead of eval\\')'))();If eval is evil, so is the function constructor.

brothercake
04-04-2003, 09:27 PM
Originally posted by liorean
Brothercake
Huh? Was that a Freudian slip (or perhaps, a Jungian slip :D)


Anyhoo - eval is not inherently evil, it's just got bad press because of a particular misuse:

myObj = eval("ID_or_name_of_object");

but, for eg:

eval(document.getElementById("body").getAttribute("onload"));

is a fair use

liorean
04-04-2003, 09:37 PM
Originally posted by brothercake
Huh? Was that a Freudian slip (or perhaps, a Jungian slip :D)

I don't know - I'm going to be a physician, not a shrink. Probably I just read the wrong name - you two seems to be the most frequent posters here, and I sometimes don't know which of you was the one responding.

As for your example, you forget again (I think it was you I had to remind of this - might have been Beetle) that event handlers might be strings as attributes, but they are really function bodies in javascript. document.body.onload(); should do it instead of eval.

cheesebagpipe
04-04-2003, 09:44 PM
We now return to ventura's thread, already in progress....

<html>
<head>
<title>untitled</title>
</head>
<body>
<form>
<input type="text" name="contact_email" onkeyup="logininfo_username.value=this.value">
<input type="text" name="logininfo_username">
</form>
</body>
</html>

brothercake
04-04-2003, 09:56 PM
Originally posted by liorean
As for your example, you forget again (I think it was you I had to remind of this - might have been Beetle) that event handlers might be strings as attributes, but they are really function bodies in javascript. document.body.onload(); should do it instead of eval.
Well yeah, but I was just illustrating an example of a valid use of eval; my bad I suppose, I should have thought of an ideal use ;)

(sorry ventura - I thought your question had been answered, and this was the after-debate)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum