JavaScript setAttribute()10 Apr 2025 | 3 min read The setAttribute() method is used to set or add an attribute to a particular element and provides a value to it. If the attribute already exists, it only set or changes the value of the attribute. So, we can also use the setAttribute() method to update the existing attribute's value. If the corresponding attribute does not exist, it will create a new attribute with the specified name and value. This method does not return any value. The attribute name automatically converts into lowercase when we use it on an HTML element. Although we can add the style attribute using the setAttribute() method, but it is recommended not to use this method for styling. For adding styles, we can use the properties of the style object that will effectively change the style. It can be clear with the following code. Incorrect way It is recommended not to use it to change the style. Correct way The correct way to change the style is given below. To get the value of an attribute, we can use the getAttribute() method, and to remove a specific attribute from an element, we can use the removeAtrribute() method. If we are adding a Boolean attribute such as disabled, then whatever the value it has, it is always considered as true. If we require to set the value of the Boolean attribute to false, we have to remove the entire attribute using the removeAttribute() method. SyntaxThe arguments of this method are not optional. Both parameters must be included when using this method. The parameter values of this method are defined as follows. Parameter ValuesattributeName: It is the name of the attribute that we want to add to an element. It cannot be left empty; i.e., it is not optional. attributeValue: It is the value of the attribute that we are adding to an element. It is also not an optional value. Let's understand how to use setAttribute() method by using some illustrations. Example1In this example, we are adding a href attribute with a value of "https://www.TpointTech.com/" to the <a> tag with id = "link". Output After the execution of the above code, the output will be - ![]() We can see that before clicking the given button, the link is not created. After clicking the button, the output will be - ![]() Now, we can see that the link is created. Example2In this example we are updating the value of a existing attribute using the setAttribute() method. Here, we are converting a textfield to a button by changing the value of type attribute from text to button. We have to click the specified button to see the effect. Output After the execution of the above code, the output will be - ![]() After clicking the button, the output will be - ![]() Example3Here, we are adding a Boolean attribute disabled to disable the specified button. If we set the value of the disabled attribute to an empty string, then it is automatically sets to true which causes the button to be disabled. Output After the execution of the above code, the output will be - ![]() After clicking the button, the output will be - ![]() Next TopicJavascript-setinterval |
We can create external JavaScript file and embed it in many html page. It provides code re usability because single JavaScript file can be used in several html pages. An external JavaScript file must be saved by .js extension. It is recommended to embed all JavaScript files...
2 min read
What is ? In JavaScript, Astro.js is a web framework which is optimized for building fast and content driven websites. It is a modern JS based framework that help to build fast content websites. In simple words, refers to JavaScript code which is used within the context...
6 min read
ES6 offers a metaproperty called new.target to identify whether a function or constructor runs using the new operator. The target attribute, dot, and new keyword build the object of the new.target. All methods have access to the new.target metaproperty. In contrast, the new.target in arrow functions...
3 min read
HTML or DOM events are widely used in JavaScript code. JavaScript code is executed with HTML/DOM events. So before learning JavaScript, let’s have some idea about events. Events Description onclick occurs when element is clicked. ondblclick occurs when element is double-clicked. onfocus occurs when an element gets focus such as button, input, textarea...
1 min read
The regex "\0" shows the available null character in the input string in JavaScript. If the null value is available, then the position of the values shows; otherwise, the "-1" value shows using the search method. We can test, search, and match the required string...
8 min read
Introduction: Merging arrays in JavaScript is a basic idea in data manipulation, allowing developers to turn disorderly data from many sources into a standard cohesive unit. Merging arrays is a routine and important operation used for one thing or another (data aggregation, simplifying code, etc.) and serves...
5 min read
JavaScript is a very popular, widely used, and easy programming language. It is popular among developers because of its versatile nature and powerful features. Mastering JavaScript requires a deep understanding of concepts and practice for problem-solving. In this article, we will discuss 10 JavaScript problems that...
10 min read
In this article, we are going to discuss JavaScript's in operator. We have heard and discussed many times about arithmetic, logical, comparison, and other operators. But have you heard or know about the in operator of JavaScript. Probably the answer is no, so let's discuss...
3 min read
? A block statement, here and there alluded to as a compound statement in JavaScript, is utilized to consolidate at least zero statements into one. The curly braces {} are utilized for this grouping. Characterizing a scope for variables and functions is the principal job of a...
4 min read
In JavaScript, manipulating strings and text is a common task for developers. One fundamental aspect of handling text is managing line breaks. Whether you're working with user input, data from APIs, or generating dynamic content, understanding how to insert, remove, or manipulate line breaks is essential....
10 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India