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 ); } } }
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.
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.
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...