element.setAttribute(attributeName, value)
<input type="password" value="test">
<button type="button">Show Password</button>
const elem = document.querySelector("input");
const button = document.querySelector("button");

button.addEventListener("click", () => { // Button click event
    /* When the button is clicked,
       the input element’s type attribute value is changed from password to text. */
    elem.setAttribute("type", "text");
});
Rendered Output Click the "Show Password" button!
<input type="text">
<button type="button">Add Test Value</button>
const elem = document.querySelector("input");
const button = document.querySelector("button");

button.addEventListener("click", () => { // Button click event
    /* When the button is clicked,
       the input element does not have a value attribute,
       but a value attribute is added and set to test. */
    elem.setAttribute("value", "test");
});
Rendered Output Click the "Add Test Value" button!
const element = document.getElementById("example-id");

// Set first attribute
element.setAttribute("alt", "Set your alt");

// Set second attribute
element.setAttribute("title", "Set your title");

// Set third attribute
element.setAttribute("src", "/images/image.jpg");
<input type="text">
<button type="button">Hide Input Box</button>
const elem = document.querySelector("input");
const button = document.querySelector("button");

button.addEventListener("click", () => { // Button click event
    /* When the button is clicked,
       the hidden attribute is added to the input element.
       The value is set to an empty string (""). */
    elem.setAttribute("hidden", "");
});
Rendered Output Click the "Hide Input Box" button!
element.setAttribute("style", "background-color:blue;");
element.style.backgroundColor = "blue";