topical media & game development

talk show tell print

#javascript-code-19-attr.js / js



  function attr(elem, name, value) {
      // Make sure that a valid name was provided
      if ( !name || name.constructor != String ) return '';
  
      // Figure out if the name is one of the weird naming cases
      name = { ‘for': 'htmlFor', 'class': 'className' }[name] || name;
  
      // If the user is setting a value, also
      if ( value != null ) {
          // Set the quick way first
          elem[name] = value;
  
          // If we can, use setAttribute
          if ( elem.setAttribute )
              elem.setAttribute(name,value);
      }
  
      // Return the value of the attribute
      return elem[name] || elem.getAttribute(name) || '';
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.