May 18, 2000 - Merging Objects with Perseverance | WebReference

May 18, 2000 - Merging Objects with Perseverance

Yehuda Shiran May 18, 2000
Merging Objects with Perseverance
Tips: May 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

The mergeAttributes() method copies all read/write attributes to the specified element. In Internet Explorer 5.0 and earlier, the syntax is as follows:

<targetObject>.mergeAttribute(sourceObject);
The parameter, sourceObject, is the object from which we copy the attributes to the object that invokes mergeAttributes(), <targetObject>, and is required. The targetObject's attributes having the same name as the sourceObject's attributes are preserved and are not overwritten. In Internet Explorer 5.0 and earlier, attributes that are read only, such as ID, are not merged. The mergeAttribute() method copies attributes, events, and styles.

The following example shows the usage of the mergeAttributes() as is supported by IE 5.0 and earlier:

<HTML>
<HEAD>
<SCRIPT>
<!--
function mergeObjects() {
  object2.mergeAttributes(object1);
  alert(object2.outerHTML);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
  <DIV ID="object1" ATTRIBUTE1="true" ATTRIBUTE2="true"
    onclick="alert('click');" onmouseover="this.style.color='#0000FF';"
    onmouseout="this.style.color='#000000';">
    This is a sample <b>DIV</b> element.
  </DIV>
  <DIV ID="object2">
    This is another sample <b>DIV</b> element.
  </DIV><BR>
  <INPUT TYPE="button" VALUE="Merge Attributes" onclick="mergeObjects()">
</BODY>
</HTML>

Move your mouse over the next two DIV elements. Notice the color change when you go over the top element. The top one supports the onmouseover, onmouseout, and onclick event handlers, while the bottom one does not. Now, click the "Merge Attributes" button below, and check again the two DIV elements above. Notice that the bottom DIV now has all the event handlers and attributes that the top one has. As a further proof, you also get an alert box with the HTML description of the bottom element. Again, you can notice all the elements and handlers that the top one has.

This is a sample DIV element.
This is another sample DIV element.