what property can you use to change an elements class name with javascript?
Change CSS Belongings Using JavaScript
- Change CSS Property With
getElementsByClassName
in JavaScript - Alter CSS Property With
getElementById
in JavaScript - Change CSS Property With
querySelector()
in JavaScript - Conclusion
HyperText Markup Language (HTML) is static and usually impaired. It ways it does not accept the capabilities to execute pieces of lawmaking based on dynamic conditions. It does non have provisions for if
condition blocks to show-hide certain HTML elements or tags if a condition is satisfied. For such scenarios, we may demand to rely on JavaScript or jQuery for changing the CSS styles of an HTML element.
To change an HTML element'south style, say a <div>
, nosotros should select the <div>
or that particular HTML element uniquely. Now, we can do this with the following functions of the Document Interface.
Modify CSS Property With getElementsByClassName
in JavaScript
getElementsByClassName
searches the unabridged HTML document and returns an assortment of all HTML elements having the class name as passed in the parameters of this office. We can also use it on an element to find the sub-elements with the specified CSS class proper noun. The syntax for getElementByClassName
is as follows.
document.getElementsByClassName("green-grade"));
Ready Style Using element.style
Once we accept identified the element uniquely, we can use the .style
or .className
methods to change its CSS styles. Refer to the following examples.
<div grade="col-doctor-12"> <div class="p-iii"> <label>Input String:</label><br> <input type="text" class="input-blueish-border" id="b1" value="120"> <button class="ml-3" onclick="changeStyle()">Change Border</push> </div> </div>
function changeStyle() { document.getElementsByClassName("input-blue-border")[0].style.borderColor = "ruddy"; }
Note that in the changeStyle()
method we query the input element with the document.getElementsByClassName("input-blue-border")
method. Information technology returns an array with selected elements. We select the first element of the array and change its bordercolor with .style.borderColor = "red"
.
Set Style Using chemical element.className
One tin employ element.className
to change diverse way parameters of an HTML element by clubbing those as a class and assigning the grade proper name to the selected element with chemical element.className
. This method is helpful, particularly in scenarios where nosotros need to brandish an mistake in an input field. In that example, nosotros demand to change the border color of the input field to red color and the inner text of the input to cherry color. Hence nosotros can club these styles as a form and assign them to the chemical element using the element.className
attribute. The following code illustrates error handling.
<div form="col-md-12"> <div class="p-3"> <characterization>Input String:</label><br> <input blazon="text" class="input-blue-border" id="b1" value="120"> <button class="ml-3" onclick="changeClass()">Change Border</push button> </div> </div>
part changeClass() { document.getElementsByClassName("input-blue-border")[0].className = "input-error"; }
The input-error
class has a couple of attributes that set the edge colour and the font colour of the input field to ruby-red.
-
getElementByClassName()
method returns an assortment of elements that qualify the grade proper name value passed every bit arguments. -
getElementByClassName()
tin be used while applying way changes to more one element by iterating over the assortment returned by this method. - One time the chemical element is selected,
element.style.<mode attribute>
sets the item style attribute to the selected element. - Similarly,
chemical element.className
helps prepare more than one style changes to the selected HTML element past clubbing those style attributes as a CSS form.
Change CSS Property With getElementById
in JavaScript
If nosotros have a unique id assigned to an HTML element, we tin query information technology and change its mode with the getElementById()
function of the Document
interface. Information technology is the most widely used method by web developers. By and large the ids assigned to a div will be kept unique so that while executing the getElementById()
function, other HTML elements exercise not get selected. The syntax for getElementById()
is equally depicted beneath.
document.getElementById("parent-1"));
As nosotros select a unique element, in this example, the style changes are easy to do. The following codes draw the ways. It is similar to those mentioned previously, with a deviation being how we query for an element. Hither, we uniquely identify HTML nodes with the chemical element id.
document.getElementById("b1").fashion.borderColor = "crimson"; document.getElementById("b1").className = "input-mistake";
- Unlike the
getElementByClassName()
, thegetElementById()
method returns only 1 object which is the node element selected by the query. - The intended HTML needs to have an id aspect for
getElementById()
method to work. - If more than one HTML node has the same id, so the
getElementById()
method volition return the first chemical element having the specified id. - Dissimilar
getElementByClassName()
, the mode changes can be applied directly on the object returned past thegetElementById()
role as it returns an object instead of an array.
Alter CSS Property With querySelector()
in JavaScript
querySelector()
method is a superset of features offered past the element selection mechanisms. Information technology has the combined power of both getElementsByClassName()
and getElementById()
methods. With this method, we tin can select the HTML chemical element the same way while writing CSS classes. It can work for selecting an element past id, by class and fifty-fifty by the HTML tag. Information technology behaves similar to the getElementById()
method in terms of the return type. querySelector()
returns merely the first HTML node element that satisfies the criteria mentioned in the parameters. The syntax used for querySelector()
is as follows:
document.querySelector("#<id of div>"); certificate.querySelector(".<css class proper name>"); document.querySelector("<HTML tag eg: div>");
Hence, hither too the manner tin be applied to the selected HTML chemical element by adding .style.borderColor
or .className
. If we use the same HTML structure and change the onclick
method to changeStyle()
, nosotros can achieve bringing the new style dynamically past adding the code equally described below in the JavaScript:
<push button class="ml-three" onclick="changeStyle()">Change Edge</button>
function changeStyle() { certificate.querySelector(".input-blue-border").manner.borderColor = "crimson"; }
Similarly, if we are to change the CSS class of the input, instead of just a style change, we need to use the .className()
attribute instead of .way.borderColor
in the above example.
<button form="ml-3" onclick="changeClass()">Change Border</push button>
function changeClass() { document.querySelector(".input-blue-border").className = "input-mistake"; }
- It is worthy of having an centre on the return blazon of
document.querySelector()
function. Instead of returning all the HTML objects satisfying the query, information technology returns the start element that fits the query condition. - The beauty of using the
querySelector()
is that we can use it in various circumstances, may information technology be querying based on the id of the HTML element or be its CSS class or fifty-fifty with the HTML tag name. - Dynamically change styles of more than one divs satisfying the query criteria may not exist possible with
querySelector()
as it returns an object instead of an array of HTML elements passing the query terms.
Decision
For changing the style of an HTML chemical element at run time, we tin can make utilise of .manner
or .className
attributes of the selected HTML element. The challenges are faced while selecting the target HTML node. There are diverse means to select HTML nodes uniquely. We can utilise the getElementsByClassName()
method, which uses the proper name of a CSS class to query the HTML node. Or uniquely select an element past assigning an id to information technology and querying it with .getElementById()
or using the multipurpose querySelector()
method that tin can fit all circumstances understanding the query based on the parameters passed to it.
Write for united states
DelftStack articles are written by software geeks like yous. If you besides would like to contribute to DelftStack by writing paid articles, you tin check the write for us page.
![Ezoic](https://go.ezoic.net/utilcave_com/img/ezoic.png)
Source: https://www.delftstack.com/howto/javascript/change-css-property-using-javascript/
0 Response to "what property can you use to change an elements class name with javascript?"
Postar um comentário