Get value of data-cod element

2

How do I get the value of the data-cod element?

$(document).ready(function() {

  $('.chatUsuariosLista').click(function() {
    $("#para").val(this.id);


  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="1" data-cod="21321321" class="row chatUsuariosLista">Nome 1</div>

<div id="2" data-cod="54654655" class="row chatUsuariosLista">Nome 2</div>

<div id="3" data-cod="79879878" class="row chatUsuariosLista">Nome 3</div>

<br>

<input id="para" type="text" value="">
<input id="projeto" type="text" value="">
    
asked by anonymous 19.08.2017 / 17:25

1 answer

3

You need to use the .data() API of jQuery, or the .dataset .

With jQuery it would be: var data = $(this).data('cod');
With native JavaScript it would be: var data = this.dataset.cod;

$(document).ready(function() {
  $('.chatUsuariosLista').click(function() {
    $("#para").val(this.dataset.cod);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="1" data-cod="21321321" class="row chatUsuariosLista">Nome 1</div>
<div id="2" data-cod="54654655" class="row chatUsuariosLista">Nome 2</div>
<div id="3" data-cod="79879878" class="row chatUsuariosLista">Nome 3</div>
<br>
<input id="para" type="text" value="">
<input id="projeto" type="text" value="">
    
19.08.2017 / 17:26