Tutorials using JS
Change border color of a div using JavaScript
In this tutorial, you shall learn how to change the border color of a div element using JavaScript.
Solution
To set or change the border color of a div element using JavaScript, we can use Element.style.borderColor
property. Get the div element and set its style.borderColor
property with the required color value.
divElement.style.borderColor = 'blue';
Program
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>
<html>
<body>
<h1>JavaScript Tutorial</h1>
<button id="myBtn">Click me</button>
<div id="myDiv">About</div>
<style>
#myDiv {
width: 100px;
height: 100px;
background: yellow;
border: 5px solid green;
}
</style>
<script>
document.getElementById("myBtn").addEventListener("click", function() {
//get the div element
const myDiv = document.getElementById("myDiv");
//set the border color for the div
myDiv.style.borderColor = 'blue';
});
</script>
</body>
</html>