0

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 - lorem - ipsum - dolor - by typing inside the three inputs respectively

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'>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
vegas
  • 101
  • 1
  • 7
  • 2
    You need to pass `event` as argument for `oninput` event handler. Also, to read the data attributes, Use `e.target.dataset.x`. `Elem.data()` is jQuery version of the same. – Rayon Sep 07 '22 at 11:39
  • 4
    Massive dupe. Please look before asking – mplungjan Sep 07 '22 at 11:40

1 Answers1

1

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'>
Webdeveloper_Jelle
  • 2,868
  • 4
  • 29
  • 55