Second Opinion

Discussion in 'JavaScript' started by ratman07, Sep 17, 2007.

  1. #1
    I can not figure out why this code is not working. Any suggestions would be appreciated.

    img = new Image();
    img.src = '/images/standard/check.gif';
    img.setAttribute('title',_lang_check );

    function boxes()
    {
    testforms = document.forms;
    for( i=0;i < testforms.length; i++ )
    {
    textareas = testforms.getElementsById('textarea');
    for( y=0; y < textareas.length; y++ )
    {
    link = document.createElement('a');
    link.setAttribute('href',"javascript:spellCheck(" + i + ", '" + textareas[y].name + "')");
    link.appendChild( img.cloneNode(true) );
    textareaParent = textareas[y].parentNode;
    textareaParent.insertBefore( link, textareas[y].nextSibling );
    }
    }
    }

     
    ratman07, Sep 17, 2007 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Here's one: If you're going to play with JavaScript, pay close attention to the (preferably FireFox) JavaScript console, that's why it's there.

    Element IDs are unique, so there can be no getElementsById function.

    When you show a section of code out of context, always explain what it's intended to do.

    Always wrap code within [ code ] [/code] tags.

    The protocol javascript: should not be used in the href attribute, no matter who told you to do it or how many times you've seen it done.
    Use the onclick event to call functions.

    All variables should be declared using the var keyword, unless they are intended to have global scope.
     
    Logic Ali, Sep 17, 2007 IP
  3. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Hi Ratman.

    You can use the getElementById function, which will return a reference to a single element, like Logic Ali says. (Note that a single reference is returned - so there's no "s" in getElemebtById.
     
    KatieK, Sep 19, 2007 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    on top of which, wouldn't this be a HELL of a lot faster and simpler if you used innerHTML?

    And I have the feeling this might also be better done via a LOT less code, but not sure. I'd have to see the corresponding HTML/CSS to be sure.

    Wait, are you trying to pull TEXTAREA tags and not the class/ID? You're cycling forms so I assume you'd want to cycle tags, not ID.

    I think you want getElementsByTagName...
     
    deathshadow, Sep 19, 2007 IP