skip to Main Content

I just need a function to return true or false if a textbox is selected. I am pretty new to JavaScript so I don’t even know if this is possible. Also preferably in JavaScript > jQuery

I have tried onclick but it didn’t work. I have also tried many other options shown in other answers but nothing seems to work.

2

Answers


  1. As already mentioned use .addEventListener('focus', () => {}) it should work

    Login or Signup to reply.
  2. You can check if the input element matches the :focus pseudo-class.

    const input = document.querySelector('input');
    setInterval(() => {
      console.log('focused?', input.matches(':focus'));
    }, 1000);
    <input/>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search