How to get this.data from an input with onkeyup

Asked

Viewed 92 times

0

I need to get the data of an input with javascript. In this way, I created an up-to-date function.

function atualizaAdicionados(e){
        console.log(e.value);
        console.log(e.data('id'));
}
<input type="text"  class="valor" name="valor_2" value="125.00" data-id="2" id="valor_2" onkeyup="atualizaAdicionados(this)">

Could someone help me or make another suggestion?

OBS.: I believe that I will not be able to work with the input ID field, because they will have several inputs that I will insert in the database (as if they were products of a sale), so each id will be value_ID. What I need is to get the ID only.

1 answer

1


Use the getAttribute to obtain the value of the attributes: https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute

function atualizaAdicionados(e){
        console.log(e.value);
        console.log(e.getAttribute('data-id'));
}
<input type="text"  class="valor" name="valor_2" value="125.00" data-id="2" id="valor_2" onkeyup="atualizaAdicionados(this)">

  • e.dataset.id is also an option.

  • 1

    yes @v.Santos is an option. I like the getAttribute because it serves any attribute other than "date-*", it would be possible to be for example "id-alternative", which would not be possible with dataset

  • You helped me, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.