I have a function that calls a modal window with a warning.

There is a form that you need to fill out, but before you begin to fill it out, you must first select a category using the select.

I want to implement the following.

If the user did not select the selector, but clicked on any of the fields, that is, made the focus, then the function of the modal window was called.

Is it possible to somehow accomplish this without registering a window call function for each field? Ie what as to whip off all form entirely?

  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash

1 answer 1

Take advantage of pop-up events - https://habrahabr.ru/post/246837/ .

(function(document){ var form = document.querySelector('form'), select = form.querySelector('#category'); form.addEventListener('focus',focused,true); function focused(e){ var target = e.target; if (target.tagName.toLowerCase() !== 'input') return; //Обрабатываем фокус только на input'ах /* Элемент на котором сработал фокус находится в переменной target */ console.log('focus', select.value); } })(document); 
 <form> <div> <select id="category"> <option>1</option> <option>2</option> <option>3</option> </select> </div> <div> <input type="text"> </div> <div> <input type="text"> </div> </form> 

JSFiddle - jsfiddle.net/1s3xsb95/

  • Do not tell me how to call modal when removing a keyboard suspension? Ie for example, if I call the modal window, by clicking or focusing on the input, then the "keyboard for typing" automatically crashes on the phone, how can this be prevented? - user199432
  • one
    function focused(e){ var target = e.target; if (target.tagName.toLowerCase() !== 'input') return; //Обрабатываем фокус только на input'ах target.blur(); //Убираем фокус с поля /* Элемент на котором сработал фокус находится в переменной target */ console.log('focus', select.value); } function focused(e){ var target = e.target; if (target.tagName.toLowerCase() !== 'input') return; //Обрабатываем фокус только на input'ах target.blur(); //Убираем фокус с поля /* Элемент на котором сработал фокус находится в переменной target */ console.log('focus', select.value); } - Petr Chalov
  • What was that? And I understood, there just in the comments the code was hidden. Thank ! Right now I will try - user199432