Help with Passing an object to SetTimeOut function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken

    Help with Passing an object to SetTimeOut function

    Hello

    I am trying to change the color of a font in a text box.

    I have been reading about and trying various examples found it this
    group, but still can't get it right.

    Here is where I am now:

    <script language="JavaS cript">
    <!--
    function spin(fillin,cou nt)
    {
    var intVal;
    setcolor(fillin ,"#fff000"); // works fine

    // but keep getting object expected error
    setTimeOut('set color(' + fillin + ',"#000fff");', 1000);

    intVal = eval(fillin.val ue) + eval(count);
    if (intVal < 0) intVal = 0;
    if (intVal > 999) intVal = 1000;
    fillin.value = intVal;
    return 0;
    }
    <!--
    function setcolor(fldnam e,clrcode)
    {
    fldname.style.c olor = clrcode;
    return 0;
    }
    //-->
    </script>

    I've tried various methods to get around this, but I think I need to
    be able to pass the object, not the name. Ideas tried:

    called setTimeOut('set color(' + fillin.name + ',"#000fff");', 1000);
    and tried getObjectByName - no good, at least
    Document.getobj ectbyname(flnam e).style.color was invalid...

    set fstring = 'setcolor(' + fillin + ',"#000fff"); ' and called
    setTimeOut(fstr ing,1000) but this didn't cut either.

    Any advice, redirection or other instructions will be appreciated.

    Thanks
    Ken
  • Agent Smith

    #2
    Re: Help with Passing an object to SetTimeOut function

    Alright let's see if I can help...
    [color=blue]
    > setTimeOut('set color(' + fillin + ',"#000fff");', 1000);[/color]

    Well the problem is what you want is to pass the object reference (fillin)
    to the setcolor method, but instead this code is coverting the object
    reference to a string (prob looks like [object, object] or something. And of
    course there are no attributes on a string so the setcolor method blows up
    when it tries to find a style attribute on this goofy string.

    Anyway to fix the problem I suggest the following based on the code you
    wrote. There are prob a hundred other variations.

    setTimeout('set color(' + fillin.id + ',"#000fff");', 1000);

    function setcolor(id, clrcolor){
    document.getEle mentById(id).st yle.color = clrcolor;
    }

    Note you may need to set an id value for the input field if it doesn't
    already have one.

    Regards
    Mike




    Comment

    • Ken

      #3
      Re: Help with Passing an object to SetTimeOut function

      Thanks for that Mike,

      I added id's to the input field (eg. id="1") but get a "Object doesn't
      support this property or method" error.

      This by itself doesn't work:

      document.getEle mentByID(fillin .id).style.colo r = "#BCBCBC";

      Thanks
      Ken




      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Michael Winter

        #4
        Re: Help with Passing an object to SetTimeOut function

        On 6 Oct 2004 08:39:28 -0700, Ken <twoshovels@yah oo.co.uk> wrote:
        [color=blue]
        > Hello
        >
        > I am trying to change the color of a font in a text box.
        >
        > I have been reading about and trying various examples found it this
        > group, but still can't get it right.
        >
        > Here is where I am now:
        >
        > <script language="JavaS cript">[/color]

        Don't use the language attribute. Not only is it deprecated, but the
        required type attribute makes it redundant.

        <script type="text/javascript">
        [color=blue]
        > <!--[/color]

        Similarly, hiding script contents is an out-dated practice. Remove the
        SGML comment delimiters.
        [color=blue]
        > function spin(fillin,cou nt)
        > {
        > var intVal;
        > setcolor(fillin ,"#fff000"); // works fine
        > // but keep getting object expected error
        > setTimeOut('set color(' + fillin + ',"#000fff");', 1000);[/color]

        As the other Mike pointed out, this will produce something like

        setTimeOut('set color([Object],"#000fff");',1 000);

        though the precise text between the square brackets will vary by browser.
        This is because the string concatenation you perform calls the toString
        method of the object which returns, by default, [Object <class name>].
        Although you were given one approach, there is another: a closure.

        When you place a function within another function, you are able to
        reference variables in the outer function, even once the outer function
        has returned. As long as the inner function survives somehow, the
        variables it references will also survive with their values intact.

        setTimeout(func tion() { // Note the case of setTimeout!
        setcolor(fillin , '#000fff');
        }, 1000);

        The function expression is an inner function of your spin function,
        referencing the argument, fillin. Because the setTimeout function retains
        this inner function, the survival requirement I mentioned above is met.

        There is a problem with this, however. Some older browsers are unable to
        accept a function reference as an argument to setTimeout. With a
        modification to the code already presented to you, you can use a
        string-based call to setTimeout:

        function setColor(obj, clr) {
        if('object' == typeof obj.style) {obj = obj.style;}
        obj.color = clr;
        }
        setColor.fromFo rm = function(form, name, clr) {
        setColor(docume nt.forms[form].elements[name], clr);
        };

        setTimeout('set Color.fromForm( ' + fillin.form.id
        + ',' + fillin.id + ', "#000fff"', 1000);

        replacing .id with .name as long as the names to be used are unique.
        Of course, old browsers (IE4/NN4/Opera 5, etc) might not be an issue for
        you, in which case feel free to use the much cleaner first version.
        [color=blue]
        > intVal = eval(fillin.val ue) + eval(count);[/color]

        Don't use eval, especially on user inputs. In this case, it's just a
        kludge.

        To coerce a string to a number, you can use unary plus (the parentheses
        are just for clarity):

        intVal = (+fillin.value) + (+count);

        Of course, you should ensure that the user has actually entered a number,
        first.
        [color=blue]
        > if (intVal < 0) intVal = 0;
        > if (intVal > 999) intVal = 1000;
        > fillin.value = intVal;
        > return 0;
        > }
        > <!--
        > function setcolor(fldnam e,clrcode)
        > {
        > fldname.style.c olor = clrcode;
        > return 0;
        > }[/color]

        Why are you returning zero from both functions? Granted, you haven't shown
        all your code, but I doubt any return values are necessary.

        [snip]
        [color=blue]
        > and tried getObjectByName - no good, at least
        > Document.getobj ectbyname(flnam e).style.color was invalid...[/color]

        There is no such method, getObjectByName , on the document object. There's
        getElementById, and getElementByNam e, but they aren't necessary. Also,
        watch the case of identifiers. Javascript is case-sensitive, and document
        is all lowercase.

        [snip]

        Hope that helps,
        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Michael Winter

          #5
          Re: Help with Passing an object to SetTimeOut function

          On 07 Oct 2004 09:49:23 GMT, Ken <twoshovels@yah oo.ru.ok> wrote:
          [color=blue]
          > I added id's to the input field (eg. id="1") but get a "Object doesn't
          > support this property or method" error.[/color]

          For one thing, id attribute values must start with a letter.
          [color=blue]
          > This by itself doesn't work:
          >
          > document.getEle mentByID(fillin .id).style.colo r = "#BCBCBC";[/color]

          For another, the "d" in getElementById must be lowercase. However, please
          read my other reply in this thread (if you haven't, already).

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          Working...