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 5 of 5

Thread: eval function??

  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Alcoy (Spain)
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    eval function??

    I think it is not a very big problem, so i think anyone colud help me. The problem is tha t i have a layer in my page. This layer contains a table with a lot of diferent colors, i want to show it in diferrent points of the page, each time for choose a color for one diferent thing.

    The problem is when i'm trying to know which is the button that have make it visible. I have thought of a hidden field in which store the name of the object, but i don't know how to return the value to this object, i think it is possible with the eval function, but i don't understand how to use it. If someone can explain the way it works or can just tell me how i can solve the problem it wolud be great. THnk you

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    eval() is used to resolve string/variable concatenations into usable code. For example, lets say that I have three inputs
    Code:
    <input type="text" name="counter1">
    <input type="text" name="counter2">
    <input type="text" name="counter3">
    Now lets say I want a quickie function to increment these fields, but based on a passed index, not the passed field name. I'd use eval for this.
    Code:
    function incrementCounter(index) {
    var obj = eval("document.forms[0].elements['counter"+index+"']");
    obj.value += 1;
    }
    
    //Sample call
    <span onClick="incrementCounter(2)">Add to counter 2</span>
    Does this explain it well enough?

  • #3
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    its usually best to avoid using eval if you can, and in this case, you don't need it.

    function incrementCounter(index) {
    var obj = document.forms[0].elements['counter'+index];
    obj.value += 1;
    }
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you joh6nn. I know that, but usban was asking about eval(), so I made up a simple example to help explain it. I realize that in actual application, it's useless, but that's not its purpose, now is it?

    Cheers

  • #5
    New Coder
    Join Date
    Jul 2002
    Location
    Alcoy (Spain)
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks!!

    Thank you Both of you for your explanaition. Finally it was a silly a mistake what was giving me the trouble.



  •  

    Posting Permissions

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