Stuck for several days

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

    #1

    Stuck for several days

    Hello, first of all it's convenient to say that I'm not an expert, neither
    nothing similar, just in case. I have been trying to execute this little
    piece of code with no results, does anyone there can detect where the bug
    is:

    var labelsArray = new Array(6);

    ----------------------------------------------------------------------------
    ----------------
    function objCreation()
    {
    for (i=0; i < labelsArray.len gth; i++)
    {
    var labelObj = {labelID:
    "document.getEl ementById('Labe l"+i+"').filter s.alpha.opacity "};
    labelsArray[i] = labelObj;
    }
    }

    function over(no)
    {
    eval(labelsArra y[no].labelID = 20);
    }

    ----------------------------------------------------------------------------
    ---------------

    Basically, the function objCreation creates 6 objects with only one
    attribute (labelID) each one and stores it into an Array called labelArray
    Then the function over(no) is loaded when the MouseOver event takes place.
    The problem is that since
    labelsArray[no].labelID is equal to the string
    document.getEle mentById('Label 0').filters.alp ha.opacity for example, it is
    not recognized as a functional statement, as a result it doesn't do anything
    if I assign the value 20 in this case.

    The website is at : http://new.vanara.com/ , if someone wants to see it.

    Thanks in advance,
    Javier.


  • Richard Cornford

    #2
    Re: Stuck for several days

    "Javier Troconis" <troconis@telus .net> wrote in message
    news:0hmWb.5429 2$7Q1.9039@clgr ps12...
    <snip>[color=blue]
    > var labelsArray = new Array(6);[/color]
    <snip>[color=blue]
    > function objCreation()
    > {
    > for (i=0; i < labelsArray.len gth; i++)
    > {
    > var labelObj = {labelID:
    > "document.getEl ementById('Labe l"+i+"').filter s.alpha.opacity "};
    > labelsArray[i] = labelObj;
    > }
    > }
    >
    > function over(no)
    > {
    > eval(labelsArra y[no].labelID = 20);[/color]

    If you are not an experienced javascript programmer and you are using
    the - eval - function you can be certain that you are doing something
    fundamentally wrong.

    <URL: http://jibbering.com/faq/#FAQ4_40 >
    [color=blue]
    > }[/color]
    <snip>

    You have omitted one very important detail and that is why. Why jump
    through such hoops and use such a convoluted method? When on the face of
    it your entire posted code could be replaced with:-

    function over(no){
    document.getEle mentById('Label "+no).filters.a lpha.opacity = 20;
    }

    - and achieve your apparent goal in the browsers where it will work.
    Or:-

    function over(no){
    var obj;
    if((document.ge tElementById)&&
    (obj = document.getEle mentById('Label "+no))&&
    (obj.filters)&&
    (obj.filters.al pha)){
    obj.filters.alp ha.opacity = 20;
    }
    }

    - Which achieves the same but won't error on the majority of web
    browsers in the way the previous version and your original code was
    destined to.

    Richard.


    Comment

    • Richard Cornford

      #3
      Re: Stuck for several days

      "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
      news:c0cv8f$7k1 $1$830fa79d@new s.demon.co.uk.. .
      <snip>[color=blue]
      >function over(no){
      > document.getEle mentById('Label "+no).filters.a lpha.opacity = 20;[/color]
      ^ ^
      document.getEle mentById("Label "+no).filters.a lpha.opacity = 20;[color=blue]
      >}[/color]
      <snip>

      Except in both cases it will work better with the same type of quote
      marks on each side of the string literal.

      Richard.


      Comment

      • Erwin Moller

        #4
        Re: Stuck for several days

        Richard Cornford wrote:
        [color=blue]
        > "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
        > news:c0cv8f$7k1 $1$830fa79d@new s.demon.co.uk.. .
        > <snip>[color=green]
        >>function over(no){
        >> document.getEle mentById('Label "+no).filters.a lpha.opacity = 20;[/color]
        > ^ ^
        > document.getEle mentById("Label "+no).filters.a lpha.opacity = 20;[color=green]
        >>}[/color]
        > <snip>
        >
        > Except in both cases it will work better with the same type of quote
        > marks on each side of the string literal.
        >
        > Richard.[/color]

        True, that often helps a lot. :P

        Comment

        Working...