• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript Change Border Color of Element

JavaScript Change Border Color of Element

January 20, 2022 Leave a Comment

We can use JavaScript to change the border color of a div using the borderColor property.

document.getElementById("div1").style.borderColor = "green";

Let’s say we have the following HTML:

<div id="div1">
  <p>We want to change the border color of the containing div.</p>
</div>

If we want to change the border color of #div1 to green, we can use the borderColor property in the following JavaScript code.

document.getElementById("div1").style.borderColor = "green";

The second argument can be any valid css color. So for example, we could use the hexadecimal value for green to change the border color to green.

document.getElementById("div1").style.borderColor = "#00FF00";

Using JavaScript to Change the Border Color of a Div with a Click

To change the border color of a div using JavaScript, we target the borderColor property with a click event.

Let’s say we have the following HTML and we want to change the border color of #div1 to green.

<style>#div1, #div2 { width: 300px; height: 100px; border: 3px solid #000; margin-bottom: 20px; padding: 5px; }</style>
<div id="div1">Div 1</div>
<div id="div2">Div 2</div>
<div id="click-me" onclick="clickFunction()">Change border color</div>

We can utilize both the borderColor property of #div1 along with an onclick event to change the border color of #div1.

Below is the Javascript code which will allow the user to be able to update the border color of the div:

function clickFunction() {
  var currDiv = document.getElementById("div1");
  currDiv.style.borderColor = "green";
}

The final code and output for this example of how to change the border color of our div using JavaScript is below:

Code Output:

Div 1
Div 2
Change border color

Full Code:

<style>#div1, #div2 { width: 300px; height: 100px; border: 3px solid #000; margin-bottom: 20px; padding: 5px; }</style>
<div id="div1">Div 1</div>
<div id="div2">Div 2</div>
<div id="click-me" onclick="clickFunction()">Change border color</div>

<script>

function clickFunction() {
  var currDiv = document.getElementById("div1");
  currDiv.style.borderColor = "green";
}

</script>

Hopefully this article has been useful for you to understand how to change the border color of an element using JavaScript.

Other Articles You'll Also Like:

  • 1.  Remove the First and Last Character From a String in JavaScript
  • 2.  JavaScript Map Size – Find Length of JavaScript Map
  • 3.  How to Call a JavaScript Function From HTML
  • 4.  Using JavaScript to get the Last Element in Array
  • 5.  Using JavaScript to Calculate Average of Array of Numbers
  • 6.  JavaScript offsetTop – Get the Top Position of Element
  • 7.  Check if Character is Uppercase in JavaScript
  • 8.  Create Array of Zeros in JavaScript
  • 9.  How to Repeat a String in JavaScript
  • 10.  How in JavaScript to Get the Day of the Week

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy