JavaScript Code

How to change border color of a div using JavaScript?

Tutorials using JS

Problem Statement

change the border color of a div element using JavaScript


to set or change the border color of a div element using JavaScript, we can use property. get the div element and set its style.borderColor property with the required color value = 'blue';


when user clicks on the Click me button, we get the div element with the id myDiv and set its border color to blue using style.borderColor property

<!DOCTYPE html>
<h1>JavaScript Tutorial</h1>
<button id="myBtn">Click me</button>
<div id="myDiv">About</div>

#myDiv {
  width: 100px;
  height: 100px;
  background: yellow;
  border: 5px solid green;

document.getElementById("myBtn").addEventListener("click", function() {
    //get the div element
    const myDiv = document.getElementById("myDiv");
    //set the border color for the div = 'blue';


Copyright @2022