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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Location
    Portugal
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 'Eval' method Question?

    In a form i have 'n' fields and i call the information of fields dynamic, that is, in a function a call the fields values width eval and it works.

    var teste = eval("document.frm." + d1 + ".value")

    The problem is, i want to change the field value and the 'Eval' method doesn't works. The code that i have is the next one:

    eval("document.frm." + d1 + ".value") = tes

    the 'd1' is the name of field on form and 'tes' is a value for example '333'

    when a execute this function is visible a error 'Cannot assign to a function result'

    The question is? Exists another way to put in a field dynamic information???

    Thanks,
    Pedro Américo

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I believe you need to do those operations separately
    Code:
    var formObj = eval("document.frm."+d1);
    formObj.value = tes;
    However, you can also use the elements collection of the form, and skip the eval() altogether
    Code:
    document.frm.elements[d1].value = tes;
    Note that this assumes that d1 is variable holding the name of the form field. The elements collection can also be referenced literally. If the form field's id or name was acutally "d1" then our code would look like this
    Code:
    document.frm.elements['d1'].value = tes;
    Last edited by beetle; 08-22-2002 at 08:49 PM.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    If you're doing an assignment with eval(), the assignment (the = and the RH operand) must be part of the string being eval()uated as well:

    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">

    var tes = '333'
    function demo(field_name) {
    eval("document.frm." + field_name + ".value = " + tes);
    }

    onload = function() {demo('d1')};

    </script>
    </head>
    <body>
    <form name="frm">
    <input type="text" name="d1">
    </form>
    </body>
    </html>


  •  

    Posting Permissions

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