[Javascript] javascript and id's

Paul Novitski paul at novitskisoftware.com
Tue Oct 4 12:51:10 CDT 2005


At 10:36 AM 10/4/2005, you wrote:
>I've shortened the function:
>
>function changeAttrib(id, attrib, newValue)
>{
>         document.getElementById(id).setAttribute(attrib,
>newValue);
>}

I would argue for a safer coding practice:

         var oEl = document.getElementById(id);
                 if (oEl) oEl.setAttribute(attrib, newValue);

...in order to avoid a javascript error message if the expected 
element doesn't exist on the page.

Paul 




More information about the Javascript mailing list