onfocus
//The onfocus event occurs when an element gets focus
Enter your name: <input type="text" onfocus="myFunction(this)">
<p>input field gets focus & a function is triggered changes the background-color.</p>
<script>
function myFunction(x) {
x.style.background = "yellow";
}
</script>
4.4
5
const form = document.getElementById('form');
form.addEventListener('focus', (event) => {
event.target.style.background = 'pink';
}, true);
form.addEventListener('blur', (event) => {
event.target.style.background = '';
}, true);
Thank you!
5
0
Are there any code examples left?
New code examples in category Other