Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Noblesville, IN
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    copy value of one field to another

    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.

    Code:
    <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;
    }
    Ed Ventura
    Graphic/Web Designer
    www.blackwatercompany.com

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    str.elements[copyTo].value = str.elements[orig].value;
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    New Coder
    Join Date
    Mar 2003
    Location
    iran
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <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">

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [editorial]

    eval == evil

    [/editorial]

    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Beetle - how about
    Code:
    (new Function([string anythingYouCanEval]))();
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmmm, creating serialized functions is a pretty rare occurance. One of the best applications I've seen is a constructor constructor.
    Code:
    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
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Brothercake (Ok, Beetle, then ) - you failed to see my point - I used it as were it eval. Try it:
    Code:
    (new Function('alert(\\'instead of eval\\')'))();
    If eval is evil, so is the function constructor.
    Last edited by liorean; 04-04-2003 at 10:38 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean
    Brothercake
    Huh? Was that a Freudian slip (or perhaps, a Jungian slip )


    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
    Last edited by brothercake; 04-04-2003 at 10:30 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Originally posted by brothercake
    Huh? Was that a Freudian slip (or perhaps, a Jungian slip )
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •