spacer
Yehuda Shiran May 19, 2000
Merging Objects
Tips: May 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

The mergeAttributes() method copies all read/write attributes to the specified element. The syntax is as follows:

<targetObject>.mergeAttributes(sourceObject [, preserveFlag]);
The first parameter, sourceObject, is the object from which we copy the attributes to the object that invokes mergeAttributes(), <targetObject>, and is required. The second parameter, preserveFlag, specifies whether to overwrite targetObject's attributes having the same name as the sourceObject's attributes (true for preserving and false for overwriting). This parameter is optional and has introduced only in IE 5.5. In Internet Explorer 5.0 and earlier, attributes that are read only, such as ID, are not merged. As of IE 5.5, you can choose not to preserve the identity of the targetObject and merge all attributes of an object, including ID and NAME. The mergeAttribute() method copies attributes, events, and styles.

The following example shows the usage of the mergeAttributes() with and without the optional preserveFlag flag. 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.

Currently, the two buttons yields the same results. The attribute ATTRIBUTE1 of the top element always overwrites the corresponding attribute of the bottom one.

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



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

Here is the code for the first section above:

<HTML>
<HEAD>
<SCRIPT>
<!--
function mergeObjectsTrue() {
  two.children[1].mergeAttributes(two.children[0], true);
  alert(two.children[1].outerHTML);
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<SPAN ID="two">
  <DIV  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  ATTRIBUTE1="false">
    This is another sample <b>DIV</b> element.
  </DIV>
  </SPAN><BR>
  <INPUT TYPE="button" VALUE="Merge Attributes -- Preserve"     onclick="mergeObjectsTrue()"><BR>
</BODY>
</HTML>


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business