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 element's data value using oninput event

Is it possible to get element’s data in the way like in the example below
or – any similar way – except addEventListener

currently I’m getting error – e is not defined

Expecting result should be – loremipsumdolor – by typing inside the three inputs respectively

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

function on_input(e){
    let a = e.target.data('x');
  console.log(a);
}
<input type='text' class='inp' oninput="on_input()" data-x='lorem'>
<input type='text' class='inp' oninput="on_input()" data-x='ipsum'>
<input type='text' class='inp' oninput="on_input()" data-x='dolor'>

>Solution :

You should add the element as parameter into the function.

function on_input(elem){
    let a = elem.getAttribute("data-x");
  console.log(a);
}
<input type='text' class='inp' oninput="on_input(this)" data-x='lorem'>
<input type='text' class='inp' oninput="on_input(this)" data-x='ipsum'>
<input type='text' class='inp' oninput="on_input(this)" data-x='dolor'>
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