You are here: Reference > JavaScript > client-side > HTML DOM > methods > mergeAttributes

mergeAttributes method

Browser support:
Copies all attributes and its values from the specified element to the current element.


object.mergeAttributes (source [, preserve]);
You can find the related objects in the Supported by objects section below.


Required. Reference to the source element that contains the attributes to be copied.
Optional. Boolean that indicates whether the copying of the name and id attributes is forbidden or not.
One of the following values:
Copy the name and id attributes, too.
Default. Do not copy the name and id attributes.

Return value:

This method has no return value.

Example HTML code 1:

This example illustrates the use of the mergeAttributes method:
    <script type="text/javascript">
        function CopyAttrs () {
            var source = document.getElementById ("source");
            var destination = document.getElementById ("destination");
            if (document.body.mergeAttributes) {        // Internet Explorer
                destination.mergeAttributes (source, true);
            } else {        // Firefox, Google Chrome, Safari, Opera
                for (var i=0; i < source.attributes.length; i++) {
                    var attr = source.attributes[i];
                    var attrName = ();
                    if (attrName != "id" && attrName != "name") {
                        destination.setAttribute (, attr.value);
    <div id="source" style="background-color:yellow">Source element</div>
    <br />
    <div id="destination" style="background-color:red">Destination element</div>
    <br /><br />
    <button onclick="CopyAttrs ();">Copy the attributes of the source element to the destination element!</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content