spacer
Yehuda Shiran February 1, 2001
Setting an Attribute Node
Tips: February 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

Unlike Internet Explorer that supports only setAttribute() and getAttribute(), Netscape 6 supports also setAttributeNode() and getAttributeNode(). The setAttributeNode() adds an attribute object to an existing DOM node. Here is the syntax:

elementNode.setAttributeNode(attributeObject);
where:

  • elementNode is a tag node. It mush have been created with the createElement() method.
  • attributeObject is an object that must have been created with the createAttribute() method.

Here is a sample code which first creates an object, nodePublishDate, that is equal to today's date. It then creates a tag element node ("P"), and finally converts this latter node to an attribute node:

var nodeBook, nodePublishDate;
nodePublishDate = document.createAttribute("PublishDate");
nodePublishDate.value = String(Date());
nodeBook = document.createElement("P");
nodeBook.setAttributeNode(nodePublishDate);

We put it in a conditional statement in the header of this tip:

NS6 = false;
if (document.all) {}
else if (document.getElementById) {
  NS6 = true;
  var nodeBook, nodePublishDate;
  nodePublishDate = document.createAttribute("PublishDate");
  nodePublishDate.value = String(Date());
  nodeBook = document.createElement("P");
  nodeBook.setAttributeNode(nodePublishDate);
}

Click the following buttons to see that indeed nodeBook is an attribute node. The name of the attribute is PublishDate and its value is today's date:

Here is how we define the buttons above:

<FORM>
<INPUT TYPE="button" VALUE="getAttribute('PublishDate')" 
onClick="javascript:if (NS6) {alert(nodeBook.getAttribute('PublishDate'))} 
else {alert('You must use Netscape 6')}">
<INPUT TYPE="button" VALUE="getAttributeNode('PublishDate').value" 
onClick="javascript:if (NS6) {alert(nodeBook.getAttributeNode('PublishDate').value)} 
else {alert('You must use Netscape 6')}">
</FORM>


People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS · Sending an HTML and Plain Text E-newsletter with ASP.NET, Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
ANSI SQL Hierarchical Data Processing Basics · Top 10 Threats to Wireless Security · Nuvio Intros NuvioFlex Virtual PBX