Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change border color of the html input when [duplicate]

For the sake of learning, I am making a login-registration system in HTML, CSS, and PHP. There would be different HTML text boxes and stuff.

What I want to achieve is when I write something in one text box, then take out my cursor and click on another textbox, I want to change the border color of the first text box to green.

I know how to change the border color through CSS but I want to do this only when such an event happens. Any help would be greatly appreciated.

like image 982
Ayush Avatar asked Nov 19 '25 23:11

Ayush


1 Answers

Attach blur event to the input.

document.querySelectorAll('input').forEach((input) => {
  input.addEventListener('blur', function() {
    this.classList.toggle('green', this.value.length > 0);
  });
});
.green {
    border: 2px solid green;
}
<input type="text">
<input type="text">
<button>Click Me</button>
like image 113
random Avatar answered Nov 22 '25 16:11

random



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!