SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Dynamic Object Pathing for generic function

    I'm trying to create a function that take sin three variables, ID, attribute, and value. The ID is the ID of the object, the attribute is the style attribute of said object that I wish to manipulate, and the value is the new value to change the attribute to. Seems simple enough...

    I've got it all so that it evaluates everything except it fail to find the object to apply.

    Code:
    function stylize(id,attr,val){
    var atr="document.getElementById("+id+").style."+attr+"="+"\""+val+"\"";
    eval(atr);
    }
    HTML looks like this:

    Code:
    <input type="text" name="query" size="30" value='' onclick="stylize('query','color','#000000')" id="query">
    I get the following error:
    'query' is undefined

    Any thoughts?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The return value of the eval expression is the val string, not the element.

    There is no reason to eval the expression, it turns your string 'query' into the variable query

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    The return value of the eval expression is the val string, not the element.

    There is no reason to eval the expression, it turns your string 'query' into the variable query
    I have to build the string (which is actually the dot notation path) dynamically and then eval it to parse as if it were code. I don't understand why that would not be necessary. Can you show me what you think the code should be?


Bookmarks

Posting Permissions

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