I can not get data-object value

3

The following problem has several inputs listed with the same css class, however I need to get a data-atributte of each one by clicking on its input to get its value from atributter:

For example

<input type="button" data-object="{"nome": "Fulano"}" class="btn btn-primary"/>
<input type="button" data-object="{"nome": "Maria"}" class="btn btn-primary"/>
<input type="button" data-object="{"nome": "José"}" class="btn btn-primary"/>
    
asked by anonymous 12.12.2017 / 12:08

1 answer

4

First of all review the HTML, see the "hierarchy" of the double quotation marks, you open and close them in the wrong way ... Then try the following:

$(document).ready(function() {
   $(".btn-primary").on('click', function(){
      alert(JSON.parse($(this).attr('data-object')).nome);
   });
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script><inputtype="button" data-object='{"nome": "Fulano"}' class="btn btn-primary"/>
<input type="button" data-object='{"nome": "Maria"}' class="btn btn-primary"/>
<input type="button" data-object='{"nome": "José"}' class="btn btn-primary"/>
    
12.12.2017 / 12:14