add style to element in javascript

To add style to an element in JavaScript, you can use the style property of the element.

Here's an example:

index.tsx
// Get the element to which style needs to be added
const element = document.getElementById("my-element");

// Add style properties to the element
element.style.color = "red";
element.style.border = "1px solid black";
218 chars
7 lines

In this example, we first get the element using document.getElementById(). We then use the style property of the element to add CSS properties like color and border.

This method of adding inline styles using JavaScript should be used sparingly and only when necessary. It's generally better to keep your styling in separate CSS files.

gistlibby LogSnag