Set and get element attribute - Node.js DOM

Node.js examples for DOM:Element

Description

Set and get element attribute

Demo Code

function SetAttribute( element, attName, attValue )
{
  if ( attValue == null || attValue.length == 0 )
    element.removeAttribute( attName, 0 ) ;      // 0 : Case Insensitive
  else/*from  ww w  .j  av a  2  s  .c  om*/
    element.setAttribute( attName, attValue, 0 ) ;  // 0 : Case Insensitive
}

function GetAttribute( element, attName, valueIfNull )
{
  var oAtt = element.attributes[attName] ;

  if ( oAtt == null || !oAtt.specified )
    return valueIfNull ? valueIfNull : '' ;

  var oValue ;
  
  if ( !( oValue = element.getAttribute( attName, 2 ) ) )
    oValue = oAtt.nodeValue ;

  return ( oValue == null ? valueIfNull : oValue ) ;
}

Related Tutorials