Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
How to change a div border color when clicked?

Working on a react/typescript project, I have a div and want to replace the border color and width of a div when it's clicked:

   <div
          className="showroom-card "
          onClick={() => setSelection({ showroom: 'Westchester' })}
        >

enter image description here

I know it should be simple but I'm pretty new to this.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

in javascript you can make but please describe more

div.style.border="1px solid #000";
about 4 years ago · Juan Pablo Isaza Report

0

With pure html/js it would be look like that:

.showroom-card {
  width: 500px;
  height: 300px;
  background-color: yellow;
  border: 2px solid red;
}

.selected {
  border: 2px solid black;
}
  <div
    id="card1"
    class="showroom-card"
    onClick="(function(divId) {
      targetDiv = document.querySelector(`#${divId}`)
      targetDiv.classList.toggle('selected') 
    })('card1');return false;"
  ></div>

But in react you must use state of component to manipulate div's style. For example, you would use divToggled variable in state of your component to render border and manipulate its color. The handler function, named handleDivClick change state and component will be rerendered:

class YourComponent extends React.Component {
  ...
  handleDivClick = () => {
    this.setState(divToggled: !this.state.divToggled)
  }

  ...
  render() {
    return (
      <div
        onClick={this.handleDivClick}
        className={`showroom-card ${this.state.divToggled ? 'selected' : ''}`}
      />
    )
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!