skip to Main Content

I have a form like this:

<form name="filter">

  <input type='number'>
  <input type="text">
  <input type="range">

</form>

I want to get the type of input i use but i do not succeed in it

$(document).on('input', 'form[name="filter"]', function () { 
     alert(this.type); // alerts undefined
});

Based on the input i use, i expect an alert like: number, text or range, but i got "undefined"
What i am doing wrong?

3

Answers


  1. You need to add parameter & use like this to get input type.

    $(document).on('input', 'form[name=filter]', function(input) {
      console.log(input.target.type)
    });
    <form name="filter">
      <input type='number'>
      <input type="text">
      <input type="range">
    </form>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    Login or Signup to reply.
  2. Alternative you can use it by getting event parameter from the function

      $(document).on("input", 'form[name="filter"]', function (event) {
           console.log(event.target.type);
      });
    
    Login or Signup to reply.
  3. $(document).on('input', 'form[name="filter"] input', function (event) {
        alert(event.target.type);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search