Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get the type of input when using on handler

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading