JavaScript

Set Multiple Attributes on an Element Using JavaScript

Attributes define additional features or properties of an HTML element, such as color, width, height, and so on. To provide an attribute to the element, name-value pairs, such as “name = value”, are used where the attribute’s value should be enclosed in quotation marks. So, to set the value of an attribute on the specified element, use the “Element.setAttribute()” method.

This post will illustrate the procedure to set the multiple attributes on an HTML element using JavaScript.

How to Set Multiple Attributes on an Element Using JavaScript?

To set multiple attributes on an element simultaneously, first, create an object with the attribute names and values. Get a list of the object’s keys as an array with the “Object.keys()” method, then, set all the attributes on the specified HTML element with the “setAttribute()” method.

Syntax

The given syntax is used for the setAttribute() method:

element.setAttribute(attrName, attrValue);

The above syntax contains two parameters: “name” and “value”.

  • attrName” is the name of the new attribute.
  • attrValue” is the value of the new attribute.
  • This method will create a new attribute and assign it a value. If the specified attribute already exists, then its value will get updated.

Use the given syntax for the Object.keys() method:

Object.keys(object)

It returns an array of a given object.

Example 1: Set Multiple Attributes on an Element Using forEach() Method With setAttribute() Method

First, create an element in the HTML file:

<button id="button">LINUXHINT</button>

Currently, the web page will look like this:

In JavaScript code, first, create an object named “elementAttributes” and add the attributes with names and values to the object. Here, we will add the style attribute, the name of the element, and the disable property for the button element:

const elementAttributes = {

style: 'background-color: rgb(153, 28, 49); color: white;',

name: 'LinuxButton',

disabled: '',

};

Now, define a function named “setMultipleAttributesonElement” where first call the “Object.keys()” method for getting the array of the object’s keys and then utilize the “forEach()” method to iterate through the array and finally call the “setAttribute()” method to set all the defined attributes on the specified HTML element.

function setMultipleAttributesonElement(elem, elemAttributes) {

Object.keys(elementAttributes).forEach(attribute => {

elem.setAttribute(attribute, elemAttributes[attribute]);

});

}

Fetch the button using its assigned id with the help of the “getElementById()” method:

const button = document.getElementById('button');

Invoke the defined function “setMultipleAttributesonElement” and pass the element as the first argument and the object of attributes as the second argument:

setMultipleAttributesonElement(button, elementAttributes);

The output shows that the multiple attributes of a button are successfully added:

You can also set multiple attributes on an element without creating a separate object for the attributes. To do this, follow the below example.

Example 2: Set Multiple Attributes on an Element Using for Loop With setAttribute() Method

Define a function with two parameters in a JavaScript file and use a for loop to set multiple attributes inside it by calling the “setAttribute()” method:

function setMultipleAttributesonElement(elem, elemAttributes) {

for(let i in elemAttributes) {

elem.setAttribute(i, elemAttributes[i]);

}

}

Retrieve the button using its assigned id:

const button = document.getElementById('button');

Call the defined function by passing the button element and multiple attributes in the form of name-value pairs:

setMultipleAttributesonElement(button, {"style": "background-color: rgb(153, 28, 49); color: white;", "disabled":"", "name": "LinuxButton"});

Output

We have compiled all the essential information related to setting the multiple attributes on an HTML element using JavaScript.

Conclusion

The JavaScript predefined setAttribute()” method is used to set single or multiple attributes for an element. To set the multiple attributes on an element, first, create an object that contains attributes in the form of names-values. Get the keys of objects in an array using the “Object.keys()” method, then set all the attributes on the specified elements with the “setAttribute()” method. In this post, we have illustrated the procedure to set multiple attributes on an HTML element using JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.