🚀 At the end of this article, I have shared two examples showing how to use the "outline" property in JavaScript.
Example:
<!DOCTYPE html> <html> <head> <style> input { border:solid 1px #f5f5f5; border-radius: 5px; padding: 10px; outline: none; /* remove outline or default border around textbox. */ } </style> </head> <body> <input type='text' id='tb'> </body> </html>
Using outline property within Pseudo :focus Class
Here's another example. You can use CSS outline property within Pseudo :focus class.
In this case the "outline" property is implemented only after the textbox gains focus. Here's an example.
<!DOCTYPE html> <html> <head> <style> input { padding: 10px; } input:focus { border: none; outline: none; /* remove the outline property and see what happens. */ } </style> </head> <body> <input type='text' id='tb'> </body> </html>
Using CSS "outline" property in JavaScript
You can use the outline property in JavaScript to remove focus border around textbox. You can add an event listner to check if "focus" is on a textbox.
<!DOCTYPE html> <html> <head> <style> input { padding: 10px; border: solid 1px #f5f5f5; } </style> </head> <body> <input type='text' id='tb' value=''> </body> <script> document.getElementById('tb').addEventListener('focus', function () { this.style.outline = 'none'; }); </script> </html>
Or, you can use "window.addEventListener()".
<body> <input type='text' id='tb' value=''> </body> <script> // Add an event listner to check if the focused element is a textbox. window.addEventListener('focus', function (e) { if (e.target.tagName === 'INPUT' && e.target.type === 'text') { // If its a textbox, set outline to "none". e.target.style.outline = 'none'; } }, true); </script>